Delete app.py
Browse files
app.py
DELETED
|
@@ -1,198 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import os
|
| 3 |
-
import requests
|
| 4 |
-
from gtts import gTTS
|
| 5 |
-
import tempfile
|
| 6 |
-
import uuid
|
| 7 |
-
import datetime
|
| 8 |
-
from fpdf import FPDF
|
| 9 |
-
import json
|
| 10 |
-
import whisper
|
| 11 |
-
|
| 12 |
-
# Load Whisper model
|
| 13 |
-
model = whisper.load_model("base")
|
| 14 |
-
|
| 15 |
-
# Gemini setup
|
| 16 |
-
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 17 |
-
|
| 18 |
-
def gemini_chat(text):
|
| 19 |
-
url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key={GEMINI_API_KEY}"
|
| 20 |
-
headers = {"Content-Type": "application/json"}
|
| 21 |
-
payload = {"contents": [{"parts": [{"text": text}]}]}
|
| 22 |
-
res = requests.post(url, headers=headers, json=payload)
|
| 23 |
-
if res.ok:
|
| 24 |
-
return res.json()["candidates"][0]["content"]["parts"][0]["text"]
|
| 25 |
-
return "[Gemini error]"
|
| 26 |
-
|
| 27 |
-
# PDF generation
|
| 28 |
-
def generate_pdf(chat_history, session_id):
|
| 29 |
-
pdf = FPDF()
|
| 30 |
-
pdf.add_page()
|
| 31 |
-
pdf.set_font("Arial", size=12)
|
| 32 |
-
pdf.set_auto_page_break(True, margin=10)
|
| 33 |
-
for turn in chat_history:
|
| 34 |
-
pdf.multi_cell(0, 10, f"You: {turn['user']}\nAI: {turn['ai']}\n")
|
| 35 |
-
path = f"{session_id}_chat.pdf"
|
| 36 |
-
pdf.output(path)
|
| 37 |
-
return path
|
| 38 |
-
|
| 39 |
-
# Main function
|
| 40 |
-
def voice_chat(audio_file, chat_history, session_id):
|
| 41 |
-
if not audio_file:
|
| 42 |
-
return None, chat_history, session_id, None
|
| 43 |
-
|
| 44 |
-
if not session_id:
|
| 45 |
-
session_id = str(uuid.uuid4())
|
| 46 |
-
|
| 47 |
-
# Transcribe
|
| 48 |
-
result = model.transcribe(audio_file)
|
| 49 |
-
user_text = result["text"]
|
| 50 |
-
|
| 51 |
-
# Gemini response
|
| 52 |
-
ai_reply = gemini_chat(user_text)
|
| 53 |
-
|
| 54 |
-
# TTS
|
| 55 |
-
tts = gTTS(ai_reply)
|
| 56 |
-
tts_path = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False)
|
| 57 |
-
tts.save(tts_path.name)
|
| 58 |
-
|
| 59 |
-
# Update chat
|
| 60 |
-
chat_history.append({"user": user_text, "ai": ai_reply})
|
| 61 |
-
|
| 62 |
-
# Format for display
|
| 63 |
-
formatted = ""
|
| 64 |
-
for turn in chat_history:
|
| 65 |
-
formatted += f"🗣️ **You**: {turn['user']}\n\n🤖 **AI**: {turn['ai']}\n\n"
|
| 66 |
-
|
| 67 |
-
return tts_path.name, chat_history, session_id, formatted
|
| 68 |
-
|
| 69 |
-
# PDF button handler
|
| 70 |
-
def download_pdf(chat_history, session_id):
|
| 71 |
-
if not chat_history or not session_id:
|
| 72 |
-
return None
|
| 73 |
-
return generate_pdf(chat_history, session_id)
|
| 74 |
-
|
| 75 |
-
# UI
|
| 76 |
-
with gr.Blocks() as demo:
|
| 77 |
-
gr.Markdown("## 🎙️ Real-Time Voice Chat with AI (Gemini)")
|
| 78 |
-
|
| 79 |
-
session_id = gr.State()
|
| 80 |
-
chat_history = gr.State([])
|
| 81 |
-
|
| 82 |
-
with gr.Row():
|
| 83 |
-
mic = gr.Audio(type="filepath", label="🎤 Speak (up to 30s)")
|
| 84 |
-
submit_btn = gr.Button("Send")
|
| 85 |
-
|
| 86 |
-
with gr.Row():
|
| 87 |
-
audio_out = gr.Audio(label="🔊 AI Response")
|
| 88 |
-
chatbox = gr.Markdown()
|
| 89 |
-
|
| 90 |
-
with gr.Row():
|
| 91 |
-
pdf_btn = gr.Button("📥 Download Chat as PDF")
|
| 92 |
-
pdf_file = gr.File()
|
| 93 |
-
|
| 94 |
-
submit_btn.click(fn=voice_chat, inputs=[mic, chat_history, session_id],
|
| 95 |
-
outputs=[audio_out, chat_history, session_id, chatbox])
|
| 96 |
-
|
| 97 |
-
pdf_btn.click(fn=download_pdf, inputs=[chat_history, session_id], outputs=[pdf_file])
|
| 98 |
-
|
| 99 |
-
demo.launch()
|
| 100 |
-
import gradio as gr
|
| 101 |
-
import os
|
| 102 |
-
import requests
|
| 103 |
-
from gtts import gTTS
|
| 104 |
-
import tempfile
|
| 105 |
-
import uuid
|
| 106 |
-
import datetime
|
| 107 |
-
from fpdf import FPDF
|
| 108 |
-
import json
|
| 109 |
-
import whisper
|
| 110 |
-
|
| 111 |
-
# Load Whisper model
|
| 112 |
-
model = whisper.load_model("base")
|
| 113 |
-
|
| 114 |
-
# Gemini setup
|
| 115 |
-
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 116 |
-
|
| 117 |
-
def gemini_chat(text):
|
| 118 |
-
url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key={GEMINI_API_KEY}"
|
| 119 |
-
headers = {"Content-Type": "application/json"}
|
| 120 |
-
payload = {"contents": [{"parts": [{"text": text}]}]}
|
| 121 |
-
res = requests.post(url, headers=headers, json=payload)
|
| 122 |
-
if res.ok:
|
| 123 |
-
return res.json()["candidates"][0]["content"]["parts"][0]["text"]
|
| 124 |
-
return "[Gemini error]"
|
| 125 |
-
|
| 126 |
-
# PDF generation
|
| 127 |
-
def generate_pdf(chat_history, session_id):
|
| 128 |
-
pdf = FPDF()
|
| 129 |
-
pdf.add_page()
|
| 130 |
-
pdf.set_font("Arial", size=12)
|
| 131 |
-
pdf.set_auto_page_break(True, margin=10)
|
| 132 |
-
for turn in chat_history:
|
| 133 |
-
pdf.multi_cell(0, 10, f"You: {turn['user']}\nAI: {turn['ai']}\n")
|
| 134 |
-
path = f"{session_id}_chat.pdf"
|
| 135 |
-
pdf.output(path)
|
| 136 |
-
return path
|
| 137 |
-
|
| 138 |
-
# Main function
|
| 139 |
-
def voice_chat(audio_file, chat_history, session_id):
|
| 140 |
-
if not audio_file:
|
| 141 |
-
return None, chat_history, session_id, None
|
| 142 |
-
|
| 143 |
-
if not session_id:
|
| 144 |
-
session_id = str(uuid.uuid4())
|
| 145 |
-
|
| 146 |
-
# Transcribe
|
| 147 |
-
result = model.transcribe(audio_file)
|
| 148 |
-
user_text = result["text"]
|
| 149 |
-
|
| 150 |
-
# Gemini response
|
| 151 |
-
ai_reply = gemini_chat(user_text)
|
| 152 |
-
|
| 153 |
-
# TTS
|
| 154 |
-
tts = gTTS(ai_reply)
|
| 155 |
-
tts_path = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False)
|
| 156 |
-
tts.save(tts_path.name)
|
| 157 |
-
|
| 158 |
-
# Update chat
|
| 159 |
-
chat_history.append({"user": user_text, "ai": ai_reply})
|
| 160 |
-
|
| 161 |
-
# Format for display
|
| 162 |
-
formatted = ""
|
| 163 |
-
for turn in chat_history:
|
| 164 |
-
formatted += f"🗣️ **You**: {turn['user']}\n\n🤖 **AI**: {turn['ai']}\n\n"
|
| 165 |
-
|
| 166 |
-
return tts_path.name, chat_history, session_id, formatted
|
| 167 |
-
|
| 168 |
-
# PDF button handler
|
| 169 |
-
def download_pdf(chat_history, session_id):
|
| 170 |
-
if not chat_history or not session_id:
|
| 171 |
-
return None
|
| 172 |
-
return generate_pdf(chat_history, session_id)
|
| 173 |
-
|
| 174 |
-
# UI
|
| 175 |
-
with gr.Blocks() as demo:
|
| 176 |
-
gr.Markdown("## 🎙️ Real-Time Voice Chat with AI (Gemini)")
|
| 177 |
-
|
| 178 |
-
session_id = gr.State()
|
| 179 |
-
chat_history = gr.State([])
|
| 180 |
-
|
| 181 |
-
with gr.Row():
|
| 182 |
-
mic = gr.Audio(type="filepath", label="🎤 Speak (up to 30s)")
|
| 183 |
-
submit_btn = gr.Button("Send")
|
| 184 |
-
|
| 185 |
-
with gr.Row():
|
| 186 |
-
audio_out = gr.Audio(label="🔊 AI Response")
|
| 187 |
-
chatbox = gr.Markdown()
|
| 188 |
-
|
| 189 |
-
with gr.Row():
|
| 190 |
-
pdf_btn = gr.Button("📥 Download Chat as PDF")
|
| 191 |
-
pdf_file = gr.File()
|
| 192 |
-
|
| 193 |
-
submit_btn.click(fn=voice_chat, inputs=[mic, chat_history, session_id],
|
| 194 |
-
outputs=[audio_out, chat_history, session_id, chatbox])
|
| 195 |
-
|
| 196 |
-
pdf_btn.click(fn=download_pdf, inputs=[chat_history, session_id], outputs=[pdf_file])
|
| 197 |
-
|
| 198 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|