Sayoojsj commited on
Commit
0ce7f49
Β·
verified Β·
1 Parent(s): e619630

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +271 -70
app.py CHANGED
@@ -1,70 +1,271 @@
1
- import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
-
5
- def respond(
6
- message,
7
- history: list[dict[str, str]],
8
- system_message,
9
- max_tokens,
10
- temperature,
11
- top_p,
12
- hf_token: gr.OAuthToken,
13
- ):
14
- """
15
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
16
- """
17
- client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
18
-
19
- messages = [{"role": "system", "content": system_message}]
20
-
21
- messages.extend(history)
22
-
23
- messages.append({"role": "user", "content": message})
24
-
25
- response = ""
26
-
27
- for message in client.chat_completion(
28
- messages,
29
- max_tokens=max_tokens,
30
- stream=True,
31
- temperature=temperature,
32
- top_p=top_p,
33
- ):
34
- choices = message.choices
35
- token = ""
36
- if len(choices) and choices[0].delta.content:
37
- token = choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
-
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- chatbot = gr.ChatInterface(
47
- respond,
48
- type="messages",
49
- additional_inputs=[
50
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
51
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
52
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
53
- gr.Slider(
54
- minimum=0.1,
55
- maximum=1.0,
56
- value=0.95,
57
- step=0.05,
58
- label="Top-p (nucleus sampling)",
59
- ),
60
- ],
61
- )
62
-
63
- with gr.Blocks() as demo:
64
- with gr.Sidebar():
65
- gr.LoginButton()
66
- chatbot.render()
67
-
68
-
69
- if __name__ == "__main__":
70
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+