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