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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 4,
|
6 |
+
"id": "f720b88f-253c-43be-868a-a7abe028fa3e",
|
7 |
+
"metadata": {},
|
8 |
+
"outputs": [
|
9 |
+
{
|
10 |
+
"name": "stderr",
|
11 |
+
"output_type": "stream",
|
12 |
+
"text": [
|
13 |
+
"2025-02-28 17:59:44,881 - __main__ - INFO - Starting Gradio interface...\n",
|
14 |
+
"2025-02-28 17:59:44,990 - httpx - INFO - HTTP Request: GET http://127.0.0.1:7860/gradio_api/startup-events \"HTTP/1.1 200 OK\"\n",
|
15 |
+
"2025-02-28 17:59:45,008 - httpx - INFO - HTTP Request: HEAD http://127.0.0.1:7860/ \"HTTP/1.1 200 OK\"\n",
|
16 |
+
"2025-02-28 17:59:45,029 - httpx - INFO - HTTP Request: GET https://api.gradio.app/pkg-version \"HTTP/1.1 200 OK\"\n"
|
17 |
+
]
|
18 |
+
},
|
19 |
+
{
|
20 |
+
"name": "stdout",
|
21 |
+
"output_type": "stream",
|
22 |
+
"text": [
|
23 |
+
"* Running on local URL: http://127.0.0.1:7860\n"
|
24 |
+
]
|
25 |
+
},
|
26 |
+
{
|
27 |
+
"name": "stderr",
|
28 |
+
"output_type": "stream",
|
29 |
+
"text": [
|
30 |
+
"2025-02-28 17:59:45,271 - httpx - INFO - HTTP Request: GET https://api.gradio.app/v3/tunnel-request \"HTTP/1.1 200 OK\"\n"
|
31 |
+
]
|
32 |
+
},
|
33 |
+
{
|
34 |
+
"name": "stdout",
|
35 |
+
"output_type": "stream",
|
36 |
+
"text": [
|
37 |
+
"* Running on public URL: https://330105ecb2254dfaaf.gradio.live\n",
|
38 |
+
"\n",
|
39 |
+
"This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from the terminal in the working directory to deploy to Hugging Face Spaces (https://huggingface.co/spaces)\n"
|
40 |
+
]
|
41 |
+
},
|
42 |
+
{
|
43 |
+
"name": "stderr",
|
44 |
+
"output_type": "stream",
|
45 |
+
"text": [
|
46 |
+
"2025-02-28 17:59:45,933 - httpx - INFO - HTTP Request: HEAD https://330105ecb2254dfaaf.gradio.live \"HTTP/1.1 200 OK\"\n"
|
47 |
+
]
|
48 |
+
},
|
49 |
+
{
|
50 |
+
"data": {
|
51 |
+
"text/html": [
|
52 |
+
"<div><iframe src=\"https://330105ecb2254dfaaf.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
53 |
+
],
|
54 |
+
"text/plain": [
|
55 |
+
"<IPython.core.display.HTML object>"
|
56 |
+
]
|
57 |
+
},
|
58 |
+
"metadata": {},
|
59 |
+
"output_type": "display_data"
|
60 |
+
},
|
61 |
+
{
|
62 |
+
"name": "stderr",
|
63 |
+
"output_type": "stream",
|
64 |
+
"text": [
|
65 |
+
"2025-02-28 18:07:54,705 - __main__ - INFO - Successfully added character: Yoda from Star wars\n",
|
66 |
+
"2025-02-28 18:08:25,224 - __main__ - INFO - Saved conversation with chat_id: 29de7618-714a-425b-a248-257bcf6e7a90\n",
|
67 |
+
"2025-02-28 18:08:33,946 - __main__ - INFO - Saved conversation with chat_id: d709edd3-6e65-4c8a-8882-605690dba50e\n",
|
68 |
+
"2025-02-28 18:08:47,947 - __main__ - INFO - Saved conversation with chat_id: 041814d9-efa8-4997-bd2c-0dd92f9ead1f\n",
|
69 |
+
"2025-02-28 18:09:28,278 - __main__ - INFO - Saved conversation with chat_id: eeb220d5-a02d-41fb-974a-b644abb9485a\n",
|
70 |
+
"2025-02-28 18:11:57,676 - __main__ - INFO - Saved conversation with chat_id: f8bd150a-3cfd-4b19-8a97-0efd27a00ad9\n",
|
71 |
+
"2025-02-28 18:14:41,302 - __main__ - INFO - Saved conversation with chat_id: f349383d-9639-4bc0-9a9c-96eaead27986\n",
|
72 |
+
"2025-02-28 18:16:25,573 - __main__ - INFO - Saved conversation with chat_id: d3f6dd09-1743-460a-a6e5-b9611f04a808\n",
|
73 |
+
"2025-02-28 18:16:57,223 - __main__ - INFO - Saved conversation with chat_id: 99cd44f0-1489-4826-b07a-af1d5e6e3d73\n",
|
74 |
+
"2025-02-28 18:17:02,278 - __main__ - INFO - Saved conversation with chat_id: 4bb62b0d-456e-4932-97a1-ddc326afb963\n",
|
75 |
+
"2025-02-28 18:17:08,259 - __main__ - INFO - Saved conversation with chat_id: b318c125-52c7-49cd-9413-e59f2d2b9c35\n",
|
76 |
+
"2025-02-28 18:17:22,582 - __main__ - INFO - Saved conversation with chat_id: 78150400-ddc4-4cbe-8383-4709c99d3cd2\n",
|
77 |
+
"2025-02-28 18:17:47,131 - __main__ - INFO - Saved conversation with chat_id: 313f1a44-c229-48a1-86e2-08f07c2e7f06\n",
|
78 |
+
"2025-02-28 18:20:37,173 - __main__ - INFO - Saved conversation with chat_id: a6f4bc2b-16b5-443d-9827-774c4d7b91bd\n"
|
79 |
+
]
|
80 |
+
}
|
81 |
+
],
|
82 |
+
"source": [
|
83 |
+
"import os\n",
|
84 |
+
"from flask import Flask\n",
|
85 |
+
"from flask_sqlalchemy import SQLAlchemy\n",
|
86 |
+
"from dotenv import load_dotenv\n",
|
87 |
+
"import gradio as gr\n",
|
88 |
+
"import requests\n",
|
89 |
+
"from contextlib import contextmanager\n",
|
90 |
+
"from datetime import datetime\n",
|
91 |
+
"import uuid\n",
|
92 |
+
"import logging\n",
|
93 |
+
"import speech_recognition as sr\n",
|
94 |
+
"from moviepy import VideoFileClip\n",
|
95 |
+
"\n",
|
96 |
+
"# Configure logging\n",
|
97 |
+
"logging.basicConfig(level=logging.INFO,\n",
|
98 |
+
" format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')\n",
|
99 |
+
"logger = logging.getLogger(__name__)\n",
|
100 |
+
"\n",
|
101 |
+
"# Load environment variables\n",
|
102 |
+
"load_dotenv()\n",
|
103 |
+
"\n",
|
104 |
+
"# Initialize Flask app and SQLAlchemy\n",
|
105 |
+
"app = Flask(__name__)\n",
|
106 |
+
"app.config['SQLALCHEMY_DATABASE_URI'] = os.getenv('DATABASE_URL', 'sqlite:///conversations.db')\n",
|
107 |
+
"app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False\n",
|
108 |
+
"db = SQLAlchemy(app)\n",
|
109 |
+
"\n",
|
110 |
+
"# Set Gemini API key\n",
|
111 |
+
"gemini_api_key = \"AIzaSyBZNNb9t18a0RVBPtch0knP3nlSNWWu4BA\" # Replace with your actual API key\n",
|
112 |
+
"gemini_api_url = \"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent\"\n",
|
113 |
+
"\n",
|
114 |
+
"# Database models\n",
|
115 |
+
"class Character(db.Model):\n",
|
116 |
+
" id = db.Column(db.Integer, primary_key=True)\n",
|
117 |
+
" name = db.Column(db.String(100), nullable=False, unique=True)\n",
|
118 |
+
" description = db.Column(db.Text, nullable=False)\n",
|
119 |
+
" prompt_template = db.Column(db.Text, nullable=False)\n",
|
120 |
+
"\n",
|
121 |
+
"class Conversation(db.Model):\n",
|
122 |
+
" __tablename__ = 'conversation'\n",
|
123 |
+
" \n",
|
124 |
+
" id = db.Column(db.Integer, primary_key=True)\n",
|
125 |
+
" character_id = db.Column(db.Integer, db.ForeignKey('character.id'), nullable=False)\n",
|
126 |
+
" user_input = db.Column(db.Text, nullable=True)\n",
|
127 |
+
" bot_response = db.Column(db.Text, nullable=False)\n",
|
128 |
+
" timestamp = db.Column(db.DateTime, default=datetime.utcnow)\n",
|
129 |
+
" chat_id = db.Column(db.String(36), nullable=True)\n",
|
130 |
+
" user_id = db.Column(db.Integer, nullable=False)\n",
|
131 |
+
"\n",
|
132 |
+
"@contextmanager\n",
|
133 |
+
"def app_context():\n",
|
134 |
+
" with app.app_context():\n",
|
135 |
+
" yield\n",
|
136 |
+
"\n",
|
137 |
+
"def add_predefined_characters():\n",
|
138 |
+
" with app_context():\n",
|
139 |
+
" characters = [\n",
|
140 |
+
" {\n",
|
141 |
+
" \"name\": \"Chuck the Clown\",\n",
|
142 |
+
" \"description\": \"A funny clown who tells jokes and entertains.\",\n",
|
143 |
+
" \"prompt_template\": \"You are Chuck the Clown, always ready with a joke and entertainment. Be upbeat, silly, and include jokes in your responses.\"\n",
|
144 |
+
" },\n",
|
145 |
+
" {\n",
|
146 |
+
" \"name\": \"Sarcastic Pirate\",\n",
|
147 |
+
" \"description\": \"A pirate with a sharp tongue and a love for treasure.\",\n",
|
148 |
+
" \"prompt_template\": \"You are a Sarcastic Pirate, ready to share your tales of adventure. Use pirate slang, be witty, sarcastic, and mention your love for treasure and the sea.\"\n",
|
149 |
+
" },\n",
|
150 |
+
" {\n",
|
151 |
+
" \"name\": \"Professor Sage\",\n",
|
152 |
+
" \"description\": \"A wise professor knowledgeable about many subjects.\",\n",
|
153 |
+
" \"prompt_template\": \"You are Professor Sage, sharing wisdom and knowledge. Be scholarly, thoughtful, and provide educational information in your responses.\"\n",
|
154 |
+
" }\n",
|
155 |
+
" ]\n",
|
156 |
+
"\n",
|
157 |
+
" for char_data in characters:\n",
|
158 |
+
" if not Character.query.filter_by(name=char_data[\"name\"]).first():\n",
|
159 |
+
" new_character = Character(\n",
|
160 |
+
" name=char_data[\"name\"],\n",
|
161 |
+
" description=char_data[\"description\"],\n",
|
162 |
+
" prompt_template=char_data[\"prompt_template\"]\n",
|
163 |
+
" )\n",
|
164 |
+
" db.session.add(new_character)\n",
|
165 |
+
" logger.info(f\"Adding predefined character: {char_data['name']}\")\n",
|
166 |
+
" \n",
|
167 |
+
" try:\n",
|
168 |
+
" db.session.commit()\n",
|
169 |
+
" except Exception as e:\n",
|
170 |
+
" db.session.rollback()\n",
|
171 |
+
" logger.error(f\"Error adding predefined characters: {e}\")\n",
|
172 |
+
"\n",
|
173 |
+
"def add_character(name, description, prompt_template):\n",
|
174 |
+
" with app_context():\n",
|
175 |
+
" try:\n",
|
176 |
+
" if Character.query.filter_by(name=name).first():\n",
|
177 |
+
" return f\"Character '{name}' already exists!\"\n",
|
178 |
+
"\n",
|
179 |
+
" new_character = Character(\n",
|
180 |
+
" name=name,\n",
|
181 |
+
" description=description,\n",
|
182 |
+
" prompt_template=prompt_template\n",
|
183 |
+
" )\n",
|
184 |
+
" db.session.add(new_character)\n",
|
185 |
+
" db.session.commit()\n",
|
186 |
+
" logger.info(f\"Successfully added character: {name}\")\n",
|
187 |
+
" return f\"Character '{name}' added successfully!\\nDescription: {description}\"\n",
|
188 |
+
" \n",
|
189 |
+
" except Exception as e:\n",
|
190 |
+
" db.session.rollback()\n",
|
191 |
+
" logger.error(f\"Error adding character: {e}\")\n",
|
192 |
+
" return f\"An error occurred while adding the character: {str(e)}\"\n",
|
193 |
+
"\n",
|
194 |
+
"def get_existing_characters():\n",
|
195 |
+
" with app_context():\n",
|
196 |
+
" try:\n",
|
197 |
+
" characters = Character.query.all()\n",
|
198 |
+
" return [(char.name, char.description) for char in characters]\n",
|
199 |
+
" except Exception as e:\n",
|
200 |
+
" logger.error(f\"Error retrieving characters: {e}\")\n",
|
201 |
+
" return [(\"Error retrieving characters\", str(e))]\n",
|
202 |
+
"\n",
|
203 |
+
"def chat_with_character(character_name, user_input, user_id, chat_id=None):\n",
|
204 |
+
" with app_context():\n",
|
205 |
+
" try:\n",
|
206 |
+
" character = Character.query.filter_by(name=character_name).first()\n",
|
207 |
+
" \n",
|
208 |
+
" if not character:\n",
|
209 |
+
" return \"Character not found.\", None\n",
|
210 |
+
" \n",
|
211 |
+
" if not chat_id:\n",
|
212 |
+
" chat_id = str(uuid.uuid4())\n",
|
213 |
+
" \n",
|
214 |
+
" # Retrieve previous conversations for context\n",
|
215 |
+
" previous_conversations = Conversation.query.filter_by(user_id=user_id).order_by(Conversation.timestamp).all()\n",
|
216 |
+
" context_prompt = \" \".join([f\"User: {conv.user_input}\\nBot: {conv.bot_response}\" for conv in previous_conversations])\n",
|
217 |
+
" prompt_template = character.prompt_template\n",
|
218 |
+
" full_prompt = f\"{prompt_template}\\n{context_prompt}\\nUser: {user_input}\\nBot:\"\n",
|
219 |
+
"\n",
|
220 |
+
" payload = {\n",
|
221 |
+
" \"contents\": [{\n",
|
222 |
+
" \"parts\": [{\"text\": full_prompt}]\n",
|
223 |
+
" }]\n",
|
224 |
+
" }\n",
|
225 |
+
"\n",
|
226 |
+
" headers = {\n",
|
227 |
+
" 'Content-Type': 'application/json'\n",
|
228 |
+
" }\n",
|
229 |
+
"\n",
|
230 |
+
" response = requests.post(\n",
|
231 |
+
" gemini_api_url,\n",
|
232 |
+
" headers=headers,\n",
|
233 |
+
" json=payload,\n",
|
234 |
+
" params={'key': gemini_api_key}\n",
|
235 |
+
" )\n",
|
236 |
+
"\n",
|
237 |
+
" if response.status_code == 200:\n",
|
238 |
+
" response_data = response.json()\n",
|
239 |
+
" if 'candidates' in response_data and len(response_data['candidates']) > 0:\n",
|
240 |
+
" bot_response = response_data['candidates'][0]['content']['parts'][0]['text']\n",
|
241 |
+
" \n",
|
242 |
+
" conversation = Conversation(\n",
|
243 |
+
" character_id=character.id,\n",
|
244 |
+
" user_input=user_input,\n",
|
245 |
+
" bot_response=bot_response,\n",
|
246 |
+
" chat_id=chat_id,\n",
|
247 |
+
" user_id=user_id\n",
|
248 |
+
" )\n",
|
249 |
+
" db.session.add(conversation)\n",
|
250 |
+
" db.session.commit()\n",
|
251 |
+
" logger.info(f\"Saved conversation with chat_id: {chat_id}\")\n",
|
252 |
+
" return bot_response, chat_id\n",
|
253 |
+
" else:\n",
|
254 |
+
" return \"An error occurred while generating content: Unexpected response format.\", chat_id\n",
|
255 |
+
" else:\n",
|
256 |
+
" logger.error(f\"Error from Gemini API: {response.json()}\")\n",
|
257 |
+
" return f\"An error occurred while generating content: {response.status_code} - {response.text}\", chat_id\n",
|
258 |
+
"\n",
|
259 |
+
" except Exception as e:\n",
|
260 |
+
" logger.error(f\"Unexpected error in chat_with_character: {e}\")\n",
|
261 |
+
" return f\"An unexpected error occurred: {str(e)}\", chat_id\n",
|
262 |
+
"\n",
|
263 |
+
"def speech_to_text(audio_file):\n",
|
264 |
+
" \"\"\"Convert audio file to text using SpeechRecognition.\"\"\"\n",
|
265 |
+
" recognizer = sr.Recognizer()\n",
|
266 |
+
" with sr.AudioFile(audio_file) as source:\n",
|
267 |
+
" audio_data = recognizer.record(source)\n",
|
268 |
+
" try:\n",
|
269 |
+
" return recognizer.recognize_google(audio_data)\n",
|
270 |
+
" except sr.UnknownValueError:\n",
|
271 |
+
" logger.error(\"Could not understand audio\")\n",
|
272 |
+
" return None\n",
|
273 |
+
" except sr.RequestError as e:\n",
|
274 |
+
" logger.error(f\"Could not request results from Google Speech Recognition service; {e}\")\n",
|
275 |
+
" return None\n",
|
276 |
+
"\n",
|
277 |
+
"def extract_audio_from_video(video_file):\n",
|
278 |
+
" \"\"\"Extract audio from video and return the audio file path.\"\"\"\n",
|
279 |
+
" audio_file_path = \"temp_audio.wav\" # Temporary file name\n",
|
280 |
+
" try:\n",
|
281 |
+
" with VideoFileClip(video_file) as video:\n",
|
282 |
+
" video.audio.write_audiofile(audio_file_path)\n",
|
283 |
+
" except Exception as e:\n",
|
284 |
+
" logger.error(f\"Error extracting audio from video: {e}\")\n",
|
285 |
+
" return None # Return None if there's an error\n",
|
286 |
+
" return audio_file_path\n",
|
287 |
+
"\n",
|
288 |
+
"def get_chat_history(user_id):\n",
|
289 |
+
" \"\"\"Retrieve chat history for a specific user ID.\"\"\"\n",
|
290 |
+
" with app_context():\n",
|
291 |
+
" conversations = Conversation.query.filter_by(user_id=user_id).order_by(Conversation.timestamp).all()\n",
|
292 |
+
" return conversations if conversations else [] # Return empty list if no conversations\n",
|
293 |
+
"\n",
|
294 |
+
"def create_interface():\n",
|
295 |
+
" with app.app_context():\n",
|
296 |
+
" add_predefined_characters() # Add predefined characters if needed\n",
|
297 |
+
" \n",
|
298 |
+
" with gr.Blocks(title=\"Character Chat System\", theme=gr.themes.Default()) as iface:\n",
|
299 |
+
" current_chat_id = gr.State(value=None) # State to track the current chat ID\n",
|
300 |
+
" user_id = gr.State(value=None) # State to track user ID\n",
|
301 |
+
" chat_messages = gr.State(value=[]) # State to store chat messages\n",
|
302 |
+
" \n",
|
303 |
+
" gr.Markdown(\n",
|
304 |
+
" \"# π Character Chat System π\",\n",
|
305 |
+
" elem_id=\"title\"\n",
|
306 |
+
" )\n",
|
307 |
+
" \n",
|
308 |
+
" with gr.Tab(\"Sign In\"):\n",
|
309 |
+
" user_id_input = gr.Textbox(label=\"User ID\", placeholder=\"Enter your User ID\", elem_id=\"user_id_input\", interactive=True, lines=2)\n",
|
310 |
+
" sign_in_btn = gr.Button(\"Sign In\", variant=\"primary\")\n",
|
311 |
+
" sign_in_response = gr.Textbox(label=\"Sign In Response\", interactive=False)\n",
|
312 |
+
"\n",
|
313 |
+
" def sign_in(user_id_input):\n",
|
314 |
+
" user_id.value = user_id_input\n",
|
315 |
+
" return f\"Welcome, {user_id_input}!\", user_id_input\n",
|
316 |
+
" \n",
|
317 |
+
" sign_in_btn.click(\n",
|
318 |
+
" fn=sign_in,\n",
|
319 |
+
" inputs=[user_id_input],\n",
|
320 |
+
" outputs=[sign_in_response]\n",
|
321 |
+
" )\n",
|
322 |
+
"\n",
|
323 |
+
" with gr.Tab(\"Admin: Add Character\"):\n",
|
324 |
+
" with gr.Row():\n",
|
325 |
+
" with gr.Column():\n",
|
326 |
+
" name_input = gr.Textbox(label=\"Character Name\", placeholder=\"Enter character name\", elem_id=\"name_input\")\n",
|
327 |
+
" description_input = gr.Textbox(label=\"Character Description\", placeholder=\"Enter character description\", elem_id=\"description_input\")\n",
|
328 |
+
" prompt_input = gr.Textbox(label=\"Prompt Template\", placeholder=\"Enter character prompt template\", elem_id=\"prompt_input\", lines=3)\n",
|
329 |
+
" add_character_btn = gr.Button(\"Add Character\", elem_id=\"add_character_btn\", variant=\"primary\")\n",
|
330 |
+
" add_character_response = gr.Textbox(label=\"Response\", interactive=False, elem_id=\"response_output\")\n",
|
331 |
+
"\n",
|
332 |
+
" add_character_btn.click(\n",
|
333 |
+
" fn=add_character,\n",
|
334 |
+
" inputs=[name_input, description_input, prompt_input],\n",
|
335 |
+
" outputs=[add_character_response]\n",
|
336 |
+
" )\n",
|
337 |
+
" \n",
|
338 |
+
" with gr.Column():\n",
|
339 |
+
" gr.Markdown(\"## π Existing Characters π\", elem_id=\"existing_chars_title\")\n",
|
340 |
+
" existing_characters = get_existing_characters()\n",
|
341 |
+
" \n",
|
342 |
+
" character_list = gr.Dataframe(\n",
|
343 |
+
" value=existing_characters,\n",
|
344 |
+
" headers=[\"Name\", \"Description\"],\n",
|
345 |
+
" interactive=False,\n",
|
346 |
+
" elem_id=\"character_list\"\n",
|
347 |
+
" )\n",
|
348 |
+
" \n",
|
349 |
+
" refresh_characters_btn = gr.Button(\"Refresh Character List\")\n",
|
350 |
+
" \n",
|
351 |
+
" def refresh_characters():\n",
|
352 |
+
" return gr.update(value=get_existing_characters())\n",
|
353 |
+
" \n",
|
354 |
+
" refresh_characters_btn.click(\n",
|
355 |
+
" fn=refresh_characters,\n",
|
356 |
+
" outputs=[character_list]\n",
|
357 |
+
" )\n",
|
358 |
+
" \n",
|
359 |
+
" with gr.Tab(\"Chat with Character\"):\n",
|
360 |
+
" with gr.Row():\n",
|
361 |
+
" character_dropdown = gr.Dropdown(\n",
|
362 |
+
" label=\"Choose Character\", \n",
|
363 |
+
" choices=[char[0] for char in get_existing_characters()],\n",
|
364 |
+
" elem_id=\"character_dropdown\"\n",
|
365 |
+
" )\n",
|
366 |
+
" \n",
|
367 |
+
" chat_id_display = gr.Textbox(\n",
|
368 |
+
" label=\"Current Chat ID\", \n",
|
369 |
+
" interactive=False,\n",
|
370 |
+
" elem_id=\"chat_id_display\"\n",
|
371 |
+
" )\n",
|
372 |
+
" \n",
|
373 |
+
" user_input = gr.Textbox(\n",
|
374 |
+
" label=\"Your Message\",\n",
|
375 |
+
" placeholder=\"Type your message or use audio input\",\n",
|
376 |
+
" elem_id=\"user_input\",\n",
|
377 |
+
" lines=2 # Reduced height for better fitting\n",
|
378 |
+
" )\n",
|
379 |
+
"\n",
|
380 |
+
" audio_input = gr.Audio(\n",
|
381 |
+
" label=\"Audio Input\",\n",
|
382 |
+
" type=\"filepath\", # Type to receive audio file\n",
|
383 |
+
" elem_id=\"audio_input\"\n",
|
384 |
+
" )\n",
|
385 |
+
"\n",
|
386 |
+
" video_input = gr.Video(\n",
|
387 |
+
" label=\"Video Input\", # No type argument needed\n",
|
388 |
+
" elem_id=\"video_input\"\n",
|
389 |
+
" )\n",
|
390 |
+
" \n",
|
391 |
+
" chat_btn = gr.Button(\"Send\", variant=\"primary\")\n",
|
392 |
+
" chat_response = gr.Chatbot(\n",
|
393 |
+
" label=\"Chat Responses\", \n",
|
394 |
+
" elem_id=\"chat_response\",\n",
|
395 |
+
" height=300 # Set fixed height for the chat display\n",
|
396 |
+
" )\n",
|
397 |
+
" \n",
|
398 |
+
" def handle_chat(character_name, user_input, audio_file, video_file, user_id, chat_messages):\n",
|
399 |
+
" if audio_file:\n",
|
400 |
+
" user_input = speech_to_text(audio_file)\n",
|
401 |
+
" if user_input is None:\n",
|
402 |
+
" return chat_messages, None\n",
|
403 |
+
" \n",
|
404 |
+
" if video_file:\n",
|
405 |
+
" audio_file_path = extract_audio_from_video(video_file)\n",
|
406 |
+
" if audio_file_path is None: # Check if audio extraction failed\n",
|
407 |
+
" chat_messages.append((\"Bot\", \"Failed to extract audio from video. Please try a different file.\"))\n",
|
408 |
+
" return chat_messages, None\n",
|
409 |
+
"\n",
|
410 |
+
" extracted_text = speech_to_text(audio_file_path)\n",
|
411 |
+
" if extracted_text:\n",
|
412 |
+
" user_input += f\" {extracted_text}\" # Append transcribed text\n",
|
413 |
+
"\n",
|
414 |
+
" chat_messages.append((\"User\", \"Video uploaded\")) # Indicate video upload\n",
|
415 |
+
"\n",
|
416 |
+
" response, new_chat_id = chat_with_character(character_name, user_input, user_id)\n",
|
417 |
+
" chat_messages.append((\"User\", user_input)) # Add user message\n",
|
418 |
+
" chat_messages.append((\"Bot\", response)) # Add bot response\n",
|
419 |
+
" return chat_messages, new_chat_id\n",
|
420 |
+
" \n",
|
421 |
+
" chat_btn.click(\n",
|
422 |
+
" fn=handle_chat,\n",
|
423 |
+
" inputs=[character_dropdown, user_input, audio_input, video_input, user_id, chat_messages],\n",
|
424 |
+
" outputs=[chat_response, current_chat_id]\n",
|
425 |
+
" )\n",
|
426 |
+
" \n",
|
427 |
+
" with gr.Tab(\"Chat History\"):\n",
|
428 |
+
" with gr.Row():\n",
|
429 |
+
" gr.Markdown(\"## π View Chat History π\")\n",
|
430 |
+
" view_history_btn = gr.Button(\"View History\", variant=\"primary\")\n",
|
431 |
+
" chat_history_display = gr.Dataframe(label=\"Chat History\", interactive=False)\n",
|
432 |
+
"\n",
|
433 |
+
" def load_chat_history(user_id):\n",
|
434 |
+
" history = get_chat_history(user_id)\n",
|
435 |
+
" return [(conv.id, f\"User: {conv.user_input}\\nBot: {conv.bot_response} at {conv.timestamp}\") for conv in history]\n",
|
436 |
+
"\n",
|
437 |
+
" view_history_btn.click(\n",
|
438 |
+
" fn=load_chat_history,\n",
|
439 |
+
" inputs=[user_id],\n",
|
440 |
+
" outputs=[chat_history_display]\n",
|
441 |
+
" )\n",
|
442 |
+
"\n",
|
443 |
+
" with gr.Tab(\"API Status\"):\n",
|
444 |
+
" with gr.Row():\n",
|
445 |
+
" gr.Markdown(\"## π API Connection Status π\")\n",
|
446 |
+
" check_api_btn = gr.Button(\"Check API Status\", variant=\"primary\")\n",
|
447 |
+
" api_status_display = gr.Textbox(label=\"API Status\", interactive=False)\n",
|
448 |
+
"\n",
|
449 |
+
" def check_api_status():\n",
|
450 |
+
" try:\n",
|
451 |
+
" response = requests.post(\n",
|
452 |
+
" gemini_api_url,\n",
|
453 |
+
" headers={'Content-Type': 'application/json'},\n",
|
454 |
+
" json={\"contents\": [{\"parts\": [{\"text\": \"Hello\"}]}]},\n",
|
455 |
+
" params={'key': gemini_api_key}\n",
|
456 |
+
" )\n",
|
457 |
+
" if response.status_code == 200:\n",
|
458 |
+
" return \"β
API connection successful! Gemini API is operational.\"\n",
|
459 |
+
" else:\n",
|
460 |
+
" return f\"β API connection failed: {response.status_code} - {response.text}\"\n",
|
461 |
+
" except Exception as e:\n",
|
462 |
+
" return f\"β API error: {str(e)}\"\n",
|
463 |
+
"\n",
|
464 |
+
" check_api_btn.click(\n",
|
465 |
+
" fn=check_api_status,\n",
|
466 |
+
" outputs=[api_status_display]\n",
|
467 |
+
" )\n",
|
468 |
+
" \n",
|
469 |
+
" return iface\n",
|
470 |
+
"\n",
|
471 |
+
"if __name__ == \"__main__\":\n",
|
472 |
+
" with app.app_context():\n",
|
473 |
+
" db.create_all() # Ensure tables are created\n",
|
474 |
+
" add_predefined_characters() # Add predefined characters if needed\n",
|
475 |
+
" \n",
|
476 |
+
" chat_interface = create_interface()\n",
|
477 |
+
" logger.info(\"Starting Gradio interface...\")\n",
|
478 |
+
" chat_interface.launch(share=True)"
|
479 |
+
]
|
480 |
+
},
|
481 |
+
{
|
482 |
+
"cell_type": "code",
|
483 |
+
"execution_count": 15,
|
484 |
+
"id": "1c43981d-405b-4ed6-b2ca-007388979345",
|
485 |
+
"metadata": {},
|
486 |
+
"outputs": [
|
487 |
+
{
|
488 |
+
"name": "stderr",
|
489 |
+
"output_type": "stream",
|
490 |
+
"text": [
|
491 |
+
"2025-02-27 13:41:30,336 - __main__ - INFO - Dropped existing tables and created new ones.\n",
|
492 |
+
"2025-02-27 13:41:30,341 - __main__ - INFO - Adding predefined character: Chuck the Clown\n",
|
493 |
+
"2025-02-27 13:41:30,344 - __main__ - INFO - Adding predefined character: Sarcastic Pirate\n",
|
494 |
+
"2025-02-27 13:41:30,345 - __main__ - INFO - Adding predefined character: Professor Sage\n",
|
495 |
+
"2025-02-27 13:41:30,375 - __main__ - INFO - Dropped existing tables and created new ones.\n",
|
496 |
+
"2025-02-27 13:41:30,377 - __main__ - INFO - Adding predefined character: Chuck the Clown\n",
|
497 |
+
"2025-02-27 13:41:30,379 - __main__ - INFO - Adding predefined character: Sarcastic Pirate\n",
|
498 |
+
"2025-02-27 13:41:30,381 - __main__ - INFO - Adding predefined character: Professor Sage\n",
|
499 |
+
"/home/zeus/miniconda3/envs/cloudspace/lib/python3.10/site-packages/gradio/components/chatbot.py:285: UserWarning: You have not specified a value for the `type` parameter. Defaulting to the 'tuples' format for chatbot messages, but this is deprecated and will be removed in a future version of Gradio. Please set type='messages' instead, which uses openai-style dictionaries with 'role' and 'content' keys.\n",
|
500 |
+
" warnings.warn(\n",
|
501 |
+
"2025-02-27 13:41:30,533 - __main__ - INFO - Starting Gradio interface...\n",
|
502 |
+
"2025-02-27 13:41:30,613 - httpx - INFO - HTTP Request: GET http://127.0.0.1:7865/gradio_api/startup-events \"HTTP/1.1 200 OK\"\n",
|
503 |
+
"2025-02-27 13:41:30,622 - httpx - INFO - HTTP Request: HEAD http://127.0.0.1:7865/ \"HTTP/1.1 200 OK\"\n",
|
504 |
+
"2025-02-27 13:41:30,689 - httpx - INFO - HTTP Request: GET https://api.gradio.app/pkg-version \"HTTP/1.1 200 OK\"\n"
|
505 |
+
]
|
506 |
+
},
|
507 |
+
{
|
508 |
+
"name": "stdout",
|
509 |
+
"output_type": "stream",
|
510 |
+
"text": [
|
511 |
+
"* Running on local URL: http://127.0.0.1:7865\n"
|
512 |
+
]
|
513 |
+
},
|
514 |
+
{
|
515 |
+
"name": "stderr",
|
516 |
+
"output_type": "stream",
|
517 |
+
"text": [
|
518 |
+
"2025-02-27 13:41:30,890 - httpx - INFO - HTTP Request: GET https://api.gradio.app/v3/tunnel-request \"HTTP/1.1 200 OK\"\n"
|
519 |
+
]
|
520 |
+
},
|
521 |
+
{
|
522 |
+
"name": "stdout",
|
523 |
+
"output_type": "stream",
|
524 |
+
"text": [
|
525 |
+
"* Running on public URL: https://6dd5e56f61ad340d48.gradio.live\n",
|
526 |
+
"\n",
|
527 |
+
"This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from the terminal in the working directory to deploy to Hugging Face Spaces (https://huggingface.co/spaces)\n"
|
528 |
+
]
|
529 |
+
},
|
530 |
+
{
|
531 |
+
"name": "stderr",
|
532 |
+
"output_type": "stream",
|
533 |
+
"text": [
|
534 |
+
"2025-02-27 13:41:31,552 - httpx - INFO - HTTP Request: HEAD https://6dd5e56f61ad340d48.gradio.live \"HTTP/1.1 200 OK\"\n"
|
535 |
+
]
|
536 |
+
},
|
537 |
+
{
|
538 |
+
"data": {
|
539 |
+
"text/html": [
|
540 |
+
"<div><iframe src=\"https://6dd5e56f61ad340d48.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
541 |
+
],
|
542 |
+
"text/plain": [
|
543 |
+
"<IPython.core.display.HTML object>"
|
544 |
+
]
|
545 |
+
},
|
546 |
+
"metadata": {},
|
547 |
+
"output_type": "display_data"
|
548 |
+
}
|
549 |
+
],
|
550 |
+
"source": []
|
551 |
+
},
|
552 |
+
{
|
553 |
+
"cell_type": "code",
|
554 |
+
"execution_count": 3,
|
555 |
+
"id": "5a39e684-3c79-4471-a036-72ec184bd766",
|
556 |
+
"metadata": {},
|
557 |
+
"outputs": [
|
558 |
+
{
|
559 |
+
"ename": "TypeError",
|
560 |
+
"evalue": "Interface.__init__() missing 2 required positional arguments: 'inputs' and 'outputs'",
|
561 |
+
"output_type": "error",
|
562 |
+
"traceback": [
|
563 |
+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
564 |
+
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
|
565 |
+
"Cell \u001b[0;32mIn[3], line 395\u001b[0m\n\u001b[1;32m 392\u001b[0m add_predefined_characters() \u001b[38;5;66;03m# Add predefined characters if needed\u001b[39;00m\n\u001b[1;32m 394\u001b[0m \u001b[38;5;66;03m# Launch the Gradio interface\u001b[39;00m\n\u001b[0;32m--> 395\u001b[0m \u001b[43mgr\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mInterface\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfn\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcreate_interface\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mlive\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m)\u001b[49m\u001b[38;5;241m.\u001b[39mlaunch()\n",
|
566 |
+
"\u001b[0;31mTypeError\u001b[0m: Interface.__init__() missing 2 required positional arguments: 'inputs' and 'outputs'"
|
567 |
+
]
|
568 |
+
}
|
569 |
+
],
|
570 |
+
"source": []
|
571 |
+
},
|
572 |
+
{
|
573 |
+
"cell_type": "code",
|
574 |
+
"execution_count": null,
|
575 |
+
"id": "c2af98a2-e994-4f0c-8d46-eb8bef565379",
|
576 |
+
"metadata": {},
|
577 |
+
"outputs": [],
|
578 |
+
"source": []
|
579 |
+
}
|
580 |
+
],
|
581 |
+
"metadata": {
|
582 |
+
"kernelspec": {
|
583 |
+
"display_name": "Python 3",
|
584 |
+
"language": "python",
|
585 |
+
"name": "python3"
|
586 |
+
},
|
587 |
+
"language_info": {
|
588 |
+
"codemirror_mode": {
|
589 |
+
"name": "ipython",
|
590 |
+
"version": 3
|
591 |
+
},
|
592 |
+
"file_extension": ".py",
|
593 |
+
"mimetype": "text/x-python",
|
594 |
+
"name": "python",
|
595 |
+
"nbconvert_exporter": "python",
|
596 |
+
"pygments_lexer": "ipython3",
|
597 |
+
"version": "3.10.10"
|
598 |
+
}
|
599 |
+
},
|
600 |
+
"nbformat": 4,
|
601 |
+
"nbformat_minor": 5
|
602 |
+
}
|