shanusherly commited on
Commit
db74b09
Β·
verified Β·
1 Parent(s): c29b700

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +164 -271
app.py CHANGED
@@ -1,271 +1,164 @@
1
- import os
2
- import re
3
- import requests
4
- import json
5
- import gradio as gr
6
-
7
- # Google Gemini imports
8
- import google.generativeai as genai
9
- from langchain_google_genai import ChatGoogleGenerativeAI
10
- from langchain_core.prompts import PromptTemplate
11
-
12
-
13
- # legacy chains + memory now live in langchain_classic
14
- from langchain_classic.chains import LLMChain
15
- from langchain_classic.memory import ConversationBufferMemory
16
-
17
-
18
- # ElevenLabs imports
19
- from elevenlabs.client import ElevenLabs
20
- from elevenlabs import save
21
-
22
- # Google Gemini API Key
23
- GEMINI_API_KEY = "YOUR_GEMINI_API_KEY"
24
-
25
- # ElevenLabs API Key
26
- ELEVENLABS_API_KEY = "YOUR_ELEVENLABS_API_KEY"
27
-
28
- # ElevenLabs Voice ID (Rachel voice by default)
29
- ELEVENLABS_VOICE_ID = "21m00Tcm4TlvDq8ikWAM"
30
-
31
- # Configure Gemini
32
- genai.configure(api_key=GEMINI_API_KEY)
33
-
34
- # Initialize ElevenLabs client
35
- elevenlabs_client = ElevenLabs(api_key=ELEVENLABS_API_KEY)
36
-
37
- print("βœ… API keys configured successfully!")
38
-
39
- template = """You are a helpful assistant to answer user queries.
40
- {chat_history}
41
- User: {user_message}
42
- Chatbot:"""
43
-
44
- prompt = PromptTemplate(
45
- input_variables=["chat_history", "user_message"],
46
- template=template
47
- )
48
-
49
- memory = ConversationBufferMemory(memory_key="chat_history")
50
-
51
- print("βœ… Prompt template created!")
52
-
53
- # Initialize Gemini model using direct Google GenerativeAI (NOT LangChain wrapper)
54
- import google.generativeai as genai
55
-
56
- # Configure the Gemini model directly
57
- gemini_model = genai.GenerativeModel('gemini-2.5-flash')
58
-
59
- # Create a custom LLM wrapper for LangChain compatibility
60
- class GeminiLLM:
61
- def __init__(self, model):
62
- self.model = model
63
- self.memory_history = []
64
-
65
- def predict(self, user_message):
66
- # Build conversation context
67
- full_prompt = f"You are a helpful assistant to answer user queries.\n"
68
- for msg in self.memory_history:
69
- full_prompt += f"{msg}\n"
70
- full_prompt += f"User: {user_message}\nChatbot:"
71
-
72
- # Generate response
73
- response = self.model.generate_content(full_prompt)
74
- answer = response.text
75
-
76
- # Update memory
77
- self.memory_history.append(f"User: {user_message}")
78
- self.memory_history.append(f"Chatbot: {answer}")
79
-
80
- # Keep only last 10 exchanges to avoid token limits
81
- if len(self.memory_history) > 20:
82
- self.memory_history = self.memory_history[-20:]
83
-
84
- return answer
85
-
86
- # Initialize the custom LLM
87
- llm_chain = GeminiLLM(gemini_model)
88
-
89
- print("βœ… Gemini LLM initialized with direct SDK!")
90
-
91
- def generate_audio_elevenlabs(text):
92
- """
93
- Generate audio using ElevenLabs API
94
- Returns audio file path or error message
95
- """
96
- try:
97
- # Generate audio
98
- audio = elevenlabs_client.generate(
99
- text=text,
100
- voice=ELEVENLABS_VOICE_ID,
101
- model="eleven_monolingual_v1" # or "eleven_multilingual_v2"
102
- )
103
-
104
- # Save audio to file
105
- output_path = f"/content/output_audio_{hash(text) % 10000}.mp3"
106
- save(audio, output_path)
107
-
108
- return {
109
- "type": "SUCCESS",
110
- "response": output_path,
111
- "message": "Audio generated successfully"
112
- }
113
- except Exception as e:
114
- return {
115
- "type": "ERROR",
116
- "response": str(e),
117
- "message": f"Audio generation failed: {str(e)}"
118
- }
119
-
120
- def generate_audio_elevenlabs_http(text):
121
- """
122
- Alternative method using direct HTTP API calls
123
- More reliable for some use cases
124
- """
125
- url = f"https://api.elevenlabs.io/v1/text-to-speech/{ELEVENLABS_VOICE_ID}"
126
-
127
- headers = {
128
- "Accept": "audio/mpeg",
129
- "Content-Type": "application/json",
130
- "xi-api-key": ELEVENLABS_API_KEY
131
- }
132
-
133
- data = {
134
- "text": text,
135
- "model_id": "eleven_monolingual_v1",
136
- "voice_settings": {
137
- "stability": 0.5,
138
- "similarity_boost": 0.5,
139
- "style": 0.5,
140
- "use_speaker_boost": True
141
- }
142
- }
143
-
144
- try:
145
- response = requests.post(url, json=data, headers=headers)
146
- response.raise_for_status()
147
-
148
- # Save audio file
149
- output_path = f"/content/output_audio_{hash(text) % 10000}.mp3"
150
- with open(output_path, 'wb') as f:
151
- f.write(response.content)
152
-
153
- return {
154
- "type": "SUCCESS",
155
- "response": output_path,
156
- "message": "Audio generated successfully"
157
- }
158
- except requests.exceptions.RequestException as e:
159
- return {
160
- "type": "ERROR",
161
- "response": str(e),
162
- "message": f"Audio generation failed: {str(e)}"
163
- }
164
-
165
- print("βœ… ElevenLabs audio functions defined!")
166
-
167
- def get_audio_reply_for_question(text):
168
- """
169
- Generate audio for the chatbot response
170
- """
171
- generated_audio_event = generate_audio_elevenlabs(text)
172
-
173
- final_response = {
174
- "audio_path": '',
175
- "message": ''
176
- }
177
-
178
- if generated_audio_event["type"] == "SUCCESS":
179
- audio_path = generated_audio_event["response"]
180
- final_response['audio_path'] = audio_path
181
- final_response['message'] = "Audio generated successfully"
182
- else:
183
- final_response['message'] = generated_audio_event['message']
184
-
185
- return final_response
186
-
187
- print("βœ… Audio reply function defined!")
188
-
189
- def get_text_response(user_message):
190
- """
191
- Get text response from Gemini
192
- """
193
- try:
194
- response = llm_chain.predict(user_message=user_message)
195
- return response
196
- except Exception as e:
197
- error_msg = f"Error in Gemini response: {str(e)}"
198
- print(error_msg)
199
- return f"Sorry, I encountered an error: {str(e)}"
200
-
201
- print("βœ… Text response function defined!")
202
-
203
- def get_text_response_and_audio_response(user_message):
204
- """
205
- Get both text response from Gemini and audio from ElevenLabs
206
- """
207
- # Get text response from Gemini
208
- text_response = get_text_response(user_message)
209
-
210
- # Generate audio for the response
211
- audio_reply = get_audio_reply_for_question(text_response)
212
-
213
- final_response = {
214
- 'text': text_response,
215
- 'audio_path': audio_reply.get('audio_path', ''),
216
- 'message': audio_reply.get('message', '')
217
- }
218
-
219
- return final_response
220
-
221
- print("βœ… Combined response function defined!")
222
-
223
- def chat_bot_response(message, history):
224
- """
225
- Main chatbot function for Gradio interface
226
- Returns tuple of (text_response, audio_file_path)
227
- """
228
- try:
229
- # Get text and audio response
230
- response = get_text_response_and_audio_response(message)
231
-
232
- text_response = response['text']
233
- audio_path = response['audio_path']
234
-
235
- if audio_path and os.path.exists(audio_path):
236
- # Return both text and audio
237
- return text_response
238
- else:
239
- # Return only text if audio fails
240
- return text_response
241
-
242
- except Exception as e:
243
- error_msg = f"Error: {str(e)}"
244
- print(error_msg)
245
- return error_msg
246
-
247
- print("βœ… Chatbot response handler defined!")
248
-
249
- demo = gr.ChatInterface(
250
- fn=chat_bot_response,
251
- title="πŸ€– Gemini + ElevenLabs Chatbot",
252
- description="Chat with Google Gemini AI with voice responses from ElevenLabs",
253
- examples=[
254
- "How are you doing?",
255
- "What are your interests?",
256
- "Tell me a short story",
257
- "What's the weather like today?",
258
- "Explain quantum computing in simple terms"
259
- ],
260
- theme=gr.themes.Soft()
261
- )
262
-
263
- print("βœ… Gradio interface created!")
264
-
265
- if __name__ == "__main__":
266
- # Launch with public link
267
- demo.launch(
268
- share=True, # Creates public link
269
- debug=True # Shows errors and logs
270
- )
271
-
 
1
+ import os
2
+ import re
3
+ import requests
4
+ import json
5
+ import gradio as gr
6
+
7
+ # Google Gemini imports
8
+ import google.generativeai as genai
9
+
10
+ # LangChain imports
11
+ from langchain_core.prompts import PromptTemplate
12
+ from langchain_classic.chains import LLMChain
13
+ from langchain_classic.memory import ConversationBufferMemory
14
+
15
+ # -----------------------------
16
+ # READ API KEYS FROM ENVIRONMENT
17
+ # -----------------------------
18
+ GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")
19
+ ELEVENLABS_API_KEY = os.environ.get("ELEVENLABS_API_KEY")
20
+
21
+ # ElevenLabs Voice ID
22
+ ELEVENLABS_VOICE_ID = "21m00Tcm4TlvDq8ikWAM"
23
+
24
+ # Configure Gemini
25
+ genai.configure(api_key=GEMINI_API_KEY)
26
+
27
+ print("βœ… API keys loaded safely from environment!")
28
+
29
+ # -----------------------------
30
+ # PROMPT + MEMORY
31
+ # -----------------------------
32
+ template = """You are a helpful assistant to answer user queries.
33
+ {chat_history}
34
+ User: {user_message}
35
+ Chatbot:"""
36
+
37
+ prompt = PromptTemplate(
38
+ input_variables=["chat_history", "user_message"],
39
+ template=template
40
+ )
41
+
42
+ memory = ConversationBufferMemory(memory_key="chat_history")
43
+
44
+ print("βœ… Prompt template created!")
45
+
46
+ # -----------------------------
47
+ # CUSTOM GEMINI WRAPPER
48
+ # -----------------------------
49
+ gemini_model = genai.GenerativeModel('gemini-2.5-flash')
50
+
51
+ class GeminiLLM:
52
+ def __init__(self, model):
53
+ self.model = model
54
+ self.memory_history = []
55
+
56
+ def predict(self, user_message):
57
+ full_prompt = "You are a helpful assistant.\n"
58
+ for msg in self.memory_history:
59
+ full_prompt += f"{msg}\n"
60
+ full_prompt += f"User: {user_message}\nChatbot:"
61
+
62
+ response = self.model.generate_content(full_prompt)
63
+ answer = response.text
64
+
65
+ # Add to memory
66
+ self.memory_history.append(f"User: {user_message}")
67
+ self.memory_history.append(f"Chatbot: {answer}")
68
+
69
+ # Keep last 20 messages
70
+ if len(self.memory_history) > 20:
71
+ self.memory_history = self.memory_history[-20:]
72
+
73
+ return answer
74
+
75
+ llm_chain = GeminiLLM(gemini_model)
76
+
77
+ print("οΏ½οΏ½ Gemini wrapper initialized!")
78
+
79
+ # -----------------------------
80
+ # ELEVENLABS AUDIO FUNCTION
81
+ # -----------------------------
82
+ def generate_audio_elevenlabs(text):
83
+ from elevenlabs.client import ElevenLabs
84
+ from elevenlabs import save
85
+
86
+ try:
87
+ client = ElevenLabs(api_key=ELEVENLABS_API_KEY)
88
+
89
+ audio = client.generate(
90
+ text=text,
91
+ voice=ELEVENLABS_VOICE_ID,
92
+ model="eleven_monolingual_v1"
93
+ )
94
+
95
+ output_path = f"/tmp/output_{abs(hash(text)) % 100000}.mp3"
96
+ save(audio, output_path)
97
+
98
+ return {
99
+ "type": "SUCCESS",
100
+ "response": output_path
101
+ }
102
+
103
+ except Exception as e:
104
+ return {
105
+ "type": "ERROR",
106
+ "response": str(e)
107
+ }
108
+
109
+ print("βœ… ElevenLabs audio function ready!")
110
+
111
+ # -----------------------------
112
+ # TEXT RESPONSE
113
+ # -----------------------------
114
+ def get_text_response(user_message):
115
+ try:
116
+ return llm_chain.predict(user_message=user_message)
117
+ except Exception as e:
118
+ return f"Error: {str(e)}"
119
+
120
+ # -----------------------------
121
+ # COMBINED RESPONSE
122
+ # -----------------------------
123
+ def get_text_response_and_audio_response(user_message):
124
+ text_response = get_text_response(user_message)
125
+ audio_reply = generate_audio_elevenlabs(text_response)
126
+
127
+ return {
128
+ "text": text_response,
129
+ "audio_path": audio_reply.get("response", "")
130
+ }
131
+
132
+ # -----------------------------
133
+ # MAIN CHATBOT RESPONSE HANDLER
134
+ # -----------------------------
135
+ def chat_bot_response(message, history):
136
+ try:
137
+ result = get_text_response_and_audio_response(message)
138
+ return result["text"]
139
+ except Exception as e:
140
+ return f"Error: {str(e)}"
141
+
142
+ print("βœ… Chatbot handler ready!")
143
+
144
+ # -----------------------------
145
+ # UI (UNCHANGED)
146
+ # -----------------------------
147
+ demo = gr.ChatInterface(
148
+ fn=chat_bot_response,
149
+ title="πŸ€– Gemini + ElevenLabs Chatbot",
150
+ description="Chat with Google Gemini AI with voice responses from ElevenLabs",
151
+ examples=[
152
+ "How are you doing?",
153
+ "What are your interests?",
154
+ "Tell me a short story",
155
+ "What's the weather like today?",
156
+ "Explain quantum computing in simple terms"
157
+ ],
158
+ theme=gr.themes.Soft()
159
+ )
160
+
161
+ print("βœ… Gradio interface created!")
162
+
163
+ if __name__ == "__main__":
164
+ demo.launch(debug=True, share=True)