AptlyDigital commited on
Commit
c8759b1
·
verified ·
1 Parent(s): 3030ea6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +205 -18
app.py CHANGED
@@ -1,27 +1,214 @@
1
  import gradio as gr
2
  from transformers import pipeline
 
3
 
4
- chatbot = pipeline("text-generation", model="Qwen/Qwen2.5-0.5B-Instruct", trust_remote_code=True)
 
 
 
 
 
5
 
6
- def respond(message, history):
7
- prompt = f"Human: {message}\nAssistant:"
8
- response = chatbot(prompt, max_new_tokens=100)[0]['generated_text']
9
- return response.split("Assistant:")[-1].strip()
10
 
11
- # Simple interface with voice
12
- with gr.Blocks() as demo:
13
- gr.Markdown("# Voice AI Assistant")
 
14
 
15
- with gr.Row():
16
- audio = gr.Audio(sources=["microphone"], type="filepath")
17
- chatbot_display = gr.Chatbot(height=400)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
- def process_audio(audio_path):
20
- if audio_path:
21
- # In production, add Whisper here
22
- return "Voice detected! Add Whisper for transcription."
23
- return "No audio"
24
 
25
- audio.change(process_audio, audio, chatbot_display)
 
 
 
26
 
27
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  from transformers import pipeline
3
+ import whisper
4
 
5
+ # Load Qwen model
6
+ chatbot = pipeline(
7
+ "text-generation",
8
+ model="Qwen/Qwen2.5-0.5B-Instruct",
9
+ trust_remote_code=True
10
+ )
11
 
12
+ # Load Whisper model
13
+ whisper_model = whisper.load_model("base")
 
 
14
 
15
+ def transcribe_audio(audio_file):
16
+ """Convert speech to text using Whisper"""
17
+ if audio_file is None:
18
+ return ""
19
 
20
+ try:
21
+ result = whisper_model.transcribe(audio_file)
22
+ return result["text"].strip()
23
+ except Exception as e:
24
+ print(f"Transcription error: {e}")
25
+ return ""
26
+
27
+ def get_ai_response(message):
28
+ """Get response from Qwen model"""
29
+ try:
30
+ prompt = f"Human: {message}\nAssistant:"
31
+
32
+ response = chatbot(
33
+ prompt,
34
+ max_new_tokens=150,
35
+ temperature=0.7,
36
+ do_sample=True
37
+ )
38
+
39
+ full_text = response[0]['generated_text']
40
+ assistant_response = full_text.split("Assistant:")[-1].strip()
41
+
42
+ return assistant_response if assistant_response else "I'm thinking..."
43
+
44
+ except Exception as e:
45
+ return f"Error: {str(e)[:100]}"
46
+
47
+ def process_voice_input(audio, chat_history):
48
+ """Process voice input - FIXED message format"""
49
+ if audio is None:
50
+ return chat_history, "", None
51
+
52
+ # Transcribe audio to text
53
+ text = transcribe_audio(audio)
54
+
55
+ if not text:
56
+ # CORRECT FORMAT: list of dicts
57
+ chat_history.append({"role": "user", "content": "Voice input"})
58
+ chat_history.append({"role": "assistant", "content": "Sorry, I couldn't understand the audio."})
59
+ return chat_history, "", None
60
+
61
+ # Get AI response
62
+ response = get_ai_response(text)
63
+
64
+ # CORRECT FORMAT: list of dicts
65
+ chat_history.append({"role": "user", "content": f"🎤 {text}"})
66
+ chat_history.append({"role": "assistant", "content": response})
67
+
68
+ return chat_history, "", None
69
+
70
+ def process_text_input(text, chat_history):
71
+ """Process text input - FIXED message format"""
72
+ if not text.strip():
73
+ return chat_history, ""
74
+
75
+ # Get AI response
76
+ response = get_ai_response(text)
77
+
78
+ # CORRECT FORMAT: list of dicts
79
+ chat_history.append({"role": "user", "content": text})
80
+ chat_history.append({"role": "assistant", "content": response})
81
+
82
+ return chat_history, ""
83
+
84
+ def clear_chat():
85
+ """Clear chat history"""
86
+ return [], []
87
+
88
+ def set_voice_mode():
89
+ """Set voice mode status"""
90
+ return "**Status:** Voice mode active - Click microphone to speak"
91
+
92
+ def set_text_mode():
93
+ """Set text mode status"""
94
+ return "**Status:** Text mode active - Type your message"
95
+
96
+ # Create the interface
97
+ with gr.Blocks(fill_height=True) as demo:
98
+
99
+ # Simple background animation
100
+ gr.HTML("""
101
+ <div style="position:fixed; top:0; left:0; width:100vw; height:100vh;
102
+ background: linear-gradient(45deg, #0a0a0f, #1a1a2e);
103
+ z-index:-1;">
104
+ </div>
105
+ """)
106
+
107
+ # Main content
108
+ with gr.Column(elem_id="main-content"):
109
+ gr.Markdown("# 🎤 Voice-Enabled AI Assistant")
110
+ gr.Markdown("Talk to the AI using your voice or type your message")
111
+
112
+ # Chat display
113
+ chatbot_display = gr.Chatbot(
114
+ label="Conversation",
115
+ height=400
116
+ )
117
+
118
+ # Voice input section
119
+ with gr.Row():
120
+ with gr.Column(scale=1):
121
+ audio_input = gr.Audio(
122
+ sources=["microphone"],
123
+ type="filepath",
124
+ label="🎤 Speak your message",
125
+ interactive=True
126
+ )
127
+
128
+ with gr.Column(scale=1):
129
+ gr.Markdown("### Or type your message:")
130
+ text_input = gr.Textbox(
131
+ label="Type here",
132
+ placeholder="Type your message...",
133
+ lines=2
134
+ )
135
+ text_submit = gr.Button("Send Text", variant="primary")
136
+
137
+ # Control buttons
138
+ with gr.Row():
139
+ clear_btn = gr.Button("Clear Chat", variant="secondary")
140
+ voice_mode_btn = gr.Button("🎤 Voice Mode", variant="primary")
141
+ text_mode_btn = gr.Button("📝 Text Mode", variant="secondary")
142
+
143
+ # Status indicator
144
+ status = gr.Markdown("**Status:** Ready - Click microphone or type to chat")
145
+
146
+ # State - initialize with welcome message
147
+ chat_history = gr.State([
148
+ {"role": "assistant", "content": "Hello! I'm your AI assistant. Speak into the microphone or type your message."}
149
+ ])
150
+
151
+ # Event handlers
152
+ # Voice input
153
+ audio_input.stop_recording(
154
+ fn=process_voice_input,
155
+ inputs=[audio_input, chat_history],
156
+ outputs=[chatbot_display, text_input, audio_input]
157
+ )
158
+
159
+ # Text input via button
160
+ text_submit.click(
161
+ fn=process_text_input,
162
+ inputs=[text_input, chat_history],
163
+ outputs=[chatbot_display, text_input]
164
+ )
165
+
166
+ # Text input via Enter key
167
+ text_input.submit(
168
+ fn=process_text_input,
169
+ inputs=[text_input, chat_history],
170
+ outputs=[chatbot_display, text_input]
171
+ )
172
+
173
+ # Clear chat
174
+ clear_btn.click(
175
+ fn=clear_chat,
176
+ outputs=[chatbot_display, chat_history]
177
+ )
178
 
179
+ # Mode buttons
180
+ voice_mode_btn.click(
181
+ fn=set_voice_mode,
182
+ outputs=[status]
183
+ )
184
 
185
+ text_mode_btn.click(
186
+ fn=set_text_mode,
187
+ outputs=[status]
188
+ )
189
 
190
+ # Launch the app
191
+ if __name__ == "__main__":
192
+ demo.launch(
193
+ server_name="0.0.0.0",
194
+ server_port=7860,
195
+ share=False,
196
+ theme=gr.themes.Soft(),
197
+ css="""
198
+ #main-content {
199
+ background: rgba(15, 15, 25, 0.85);
200
+ backdrop-filter: blur(10px);
201
+ padding: 30px;
202
+ border-radius: 20px;
203
+ border: 1px solid rgba(255, 255, 255, 0.1);
204
+ max-width: 800px;
205
+ margin: 50px auto;
206
+ box-shadow: 0 20px 40px rgba(0, 0, 0, 0.3);
207
+ }
208
+
209
+ .gradio-container {
210
+ background: transparent !important;
211
+ min-height: 100vh !important;
212
+ }
213
+ """
214
+ )