ACloudCenter commited on
Commit
36f9277
Β·
1 Parent(s): 4727045

Fix: chatbot message format and layout improvements

Browse files
Files changed (1) hide show
  1. app.py +37 -33
app.py CHANGED
@@ -56,7 +56,8 @@ def transcribe_audio(audio_filepath):
56
 
57
  # Convert output IDs to text
58
  transcript = model.tokenizer.ids_to_text(output_ids[0].cpu())
59
- return transcript, transcript, [], 0 # Return empty history and reset counter
 
60
 
61
 
62
  # Enhanced Q&A function with conversation history
@@ -66,14 +67,15 @@ def answer_question_with_history(transcript, question, history, qa_count):
66
  return history, qa_count, "Please transcribe audio first"
67
 
68
  if qa_count >= 5:
69
- history.append((question, "You've reached the maximum of 5 questions for this transcript. Please transcribe new audio or select another example to continue."))
 
70
  return history, qa_count, ""
71
 
72
  # Build context from history for better continuity
73
  context = ""
74
- for h_question, h_answer in history[-2:]: # Use last 2 exchanges for context
75
- if h_question: # Skip the initial summary
76
- context += f"Previous question: {h_question}\nAnswer: {h_answer[:100]}...\n"
77
 
78
  with torch.inference_mode(), model.llm.disable_adapter():
79
  prompt = f"{context}Current question: {question}\n\nTranscript:\n{transcript}"
@@ -88,11 +90,12 @@ def answer_question_with_history(transcript, question, history, qa_count):
88
 
89
  # Add follow-up prompt if under 5 questions
90
  if qa_count < 4:
91
- answer += f"\n\n**Question {qa_count + 1}/5 - What else would you like to know?**"
92
  else:
93
- answer += "\n\n**This is your final question for this transcript.**"
94
 
95
- history.append((question, answer))
 
96
  return history, qa_count + 1, ""
97
 
98
  # Build the Gradio interface
@@ -107,39 +110,40 @@ with gr.Blocks(theme=theme) as demo:
107
 
108
  with gr.Row():
109
  with gr.Column(scale=1):
110
- gr.Markdown("### πŸ“Š Audio Input")
111
  audio_input = gr.Audio(
112
  sources=["microphone", "upload"],
113
  type="filepath",
114
  label="Record/Upload Audio (MP3, WAV, M4A, etc.)"
115
  )
116
  transcribe_btn = gr.Button("Transcribe Audio", variant="primary", size="lg")
117
-
118
- gr.Markdown("### πŸ“ Transcript")
119
- transcript_output = gr.Textbox(
120
- label="",
121
- lines=15,
122
- placeholder="Transcript will appear here after clicking 'Transcribe Audio'..."
123
- )
124
 
125
  with gr.Column(scale=1):
126
- gr.Markdown("### πŸ’¬ Interactive Q&A")
127
- chatbot = gr.Chatbot(
128
- type="messages",
129
- height=500,
130
- label="Conversation History",
131
- bubble_full_width=False
132
  )
133
-
134
- with gr.Row():
135
- question_input = gr.Textbox(
136
- label="Your Question",
137
- placeholder="e.g., What was the main topic? Why did they say that?",
138
- scale=4
139
- )
140
- ask_btn = gr.Button("Ask", variant="primary", scale=1)
141
-
142
- clear_chat_btn = gr.Button("Clear Chat", variant="secondary", size="sm")
 
 
 
 
 
 
 
 
143
 
144
  gr.Markdown("""
145
  ### Example Questions to Try:
@@ -170,7 +174,7 @@ with gr.Blocks(theme=theme) as demo:
170
  )
171
 
172
  clear_chat_btn.click(
173
- fn=lambda t: ([(None, f"**Summary:** {(t)}\n\n**What else would you like to know?**")] if t else [], 1 if t else 0),
174
  inputs=[transcript_state],
175
  outputs=[chatbot, qa_counter]
176
  )
 
56
 
57
  # Convert output IDs to text
58
  transcript = model.tokenizer.ids_to_text(output_ids[0].cpu())
59
+ initial_message = [{"role": "assistant", "content": f"Transcript ready. Ask me questions about it."}]
60
+ return transcript, transcript, initial_message, 0
61
 
62
 
63
  # Enhanced Q&A function with conversation history
 
67
  return history, qa_count, "Please transcribe audio first"
68
 
69
  if qa_count >= 5:
70
+ history.append({"role": "user", "content": question})
71
+ history.append({"role": "assistant", "content": "You've reached the maximum of 5 questions for this transcript. Please transcribe new audio to continue."})
72
  return history, qa_count, ""
73
 
74
  # Build context from history for better continuity
75
  context = ""
76
+ for msg in history[-4:]: # Use last 2 exchanges for context
77
+ if msg.get("role") == "user":
78
+ context += f"Previous question: {msg['content']}\n"
79
 
80
  with torch.inference_mode(), model.llm.disable_adapter():
81
  prompt = f"{context}Current question: {question}\n\nTranscript:\n{transcript}"
 
90
 
91
  # Add follow-up prompt if under 5 questions
92
  if qa_count < 4:
93
+ answer += f"\n\nQuestion {qa_count + 1}/5 - What else would you like to know?"
94
  else:
95
+ answer += "\n\nThis is your final question for this transcript."
96
 
97
+ history.append({"role": "user", "content": question})
98
+ history.append({"role": "assistant", "content": answer})
99
  return history, qa_count + 1, ""
100
 
101
  # Build the Gradio interface
 
110
 
111
  with gr.Row():
112
  with gr.Column(scale=1):
113
+ gr.Markdown("### Audio Input")
114
  audio_input = gr.Audio(
115
  sources=["microphone", "upload"],
116
  type="filepath",
117
  label="Record/Upload Audio (MP3, WAV, M4A, etc.)"
118
  )
119
  transcribe_btn = gr.Button("Transcribe Audio", variant="primary", size="lg")
 
 
 
 
 
 
 
120
 
121
  with gr.Column(scale=1):
122
+ gr.Markdown("### Transcript")
123
+ transcript_output = gr.Textbox(
124
+ label="",
125
+ lines=10,
126
+ placeholder="Transcript will appear here after clicking 'Transcribe Audio'...",
127
+ max_lines=10
128
  )
129
+
130
+ gr.Markdown("### Interactive Q&A")
131
+ chatbot = gr.Chatbot(
132
+ type="messages",
133
+ height=400,
134
+ label="Conversation History",
135
+ bubble_full_width=False
136
+ )
137
+
138
+ with gr.Row():
139
+ question_input = gr.Textbox(
140
+ label="Your Question",
141
+ placeholder="e.g., What was the main topic? Why did they say that?",
142
+ scale=4
143
+ )
144
+ ask_btn = gr.Button("Ask", variant="primary", scale=1)
145
+
146
+ clear_chat_btn = gr.Button("Clear Chat", variant="secondary")
147
 
148
  gr.Markdown("""
149
  ### Example Questions to Try:
 
174
  )
175
 
176
  clear_chat_btn.click(
177
+ fn=lambda t: ([{"role": "assistant", "content": "Chat cleared. Ask me questions about the transcript."}] if t else [], 1 if t else 0),
178
  inputs=[transcript_state],
179
  outputs=[chatbot, qa_counter]
180
  )