Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
#
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
template
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
}
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
"
|
| 129 |
-
"
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|