Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 whisper
|
| 10 |
+
|
| 11 |
+
# Whisper for transcription
|
| 12 |
+
model = whisper.load_model("base")
|
| 13 |
+
|
| 14 |
+
# OpenRouter chat
|
| 15 |
+
def ai_chat(text):
|
| 16 |
+
url = "https://openrouter.ai/api/v1/chat/completions"
|
| 17 |
+
headers = {
|
| 18 |
+
"Authorization": f"Bearer {os.getenv('OPENROUTER_API_KEY')}",
|
| 19 |
+
"Content-Type": "application/json"
|
| 20 |
+
}
|
| 21 |
+
payload = {
|
| 22 |
+
"model": "mistralai/mistral-7b-instruct",
|
| 23 |
+
"messages": [{"role": "user", "content": text}]
|
| 24 |
+
}
|
| 25 |
+
res = requests.post(url, headers=headers, json=payload)
|
| 26 |
+
if res.ok:
|
| 27 |
+
return res.json()["choices"][0]["message"]["content"]
|
| 28 |
+
return "[OpenRouter error]"
|
| 29 |
+
|
| 30 |
+
# PDF generation
|
| 31 |
+
def generate_pdf(chat_history, session_id):
|
| 32 |
+
pdf = FPDF()
|
| 33 |
+
pdf.add_page()
|
| 34 |
+
pdf.set_font("Arial", size=12)
|
| 35 |
+
for turn in chat_history:
|
| 36 |
+
pdf.multi_cell(0, 10, f"You: {turn['user']}\nAI: {turn['ai']}\n")
|
| 37 |
+
path = f"{session_id}_chat.pdf"
|
| 38 |
+
pdf.output(path)
|
| 39 |
+
return path
|
| 40 |
+
|
| 41 |
+
# Main voice chat function
|
| 42 |
+
def voice_chat(audio_file, chat_history, session_id):
|
| 43 |
+
if not audio_file:
|
| 44 |
+
return None, chat_history, session_id, None
|
| 45 |
+
|
| 46 |
+
if not session_id:
|
| 47 |
+
session_id = str(uuid.uuid4())
|
| 48 |
+
|
| 49 |
+
result = model.transcribe(audio_file)
|
| 50 |
+
user_text = result["text"]
|
| 51 |
+
ai_reply = ai_chat(user_text)
|
| 52 |
+
|
| 53 |
+
# TTS
|
| 54 |
+
tts = gTTS(ai_reply)
|
| 55 |
+
tts_path = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False)
|
| 56 |
+
tts.save(tts_path.name)
|
| 57 |
+
|
| 58 |
+
# Update chat history
|
| 59 |
+
chat_history.append({"user": user_text, "ai": ai_reply})
|
| 60 |
+
|
| 61 |
+
# Build display text
|
| 62 |
+
display = ""
|
| 63 |
+
for turn in chat_history:
|
| 64 |
+
display += f"π§βπ¬ **You**: {turn['user']}\n\nπ€ **AI**: {turn['ai']}\n\n"
|
| 65 |
+
|
| 66 |
+
return tts_path.name, chat_history, session_id, display
|
| 67 |
+
|
| 68 |
+
# PDF download
|
| 69 |
+
def download_pdf(chat_history, session_id):
|
| 70 |
+
if not chat_history or not session_id:
|
| 71 |
+
return None
|
| 72 |
+
return generate_pdf(chat_history, session_id)
|
| 73 |
+
|
| 74 |
+
# === UI ===
|
| 75 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 76 |
+
gr.Markdown("## π€ Chat with AI (Voice + Text, Powered by OpenRouter)")
|
| 77 |
+
|
| 78 |
+
session_id = gr.State()
|
| 79 |
+
chat_history = gr.State([])
|
| 80 |
+
|
| 81 |
+
chatbox = gr.Markdown("")
|
| 82 |
+
|
| 83 |
+
with gr.Row():
|
| 84 |
+
mic = gr.Audio(label="", type="filepath", show_label=False, interactive=True)
|
| 85 |
+
send_btn = gr.Button("ποΈ Speak", variant="primary")
|
| 86 |
+
|
| 87 |
+
with gr.Row():
|
| 88 |
+
audio_out = gr.Audio(label="π AI Voice Reply", interactive=False)
|
| 89 |
+
|
| 90 |
+
with gr.Row():
|
| 91 |
+
download_btn = gr.Button("π₯ Download Chat as PDF")
|
| 92 |
+
download_link = gr.File()
|
| 93 |
+
|
| 94 |
+
send_btn.click(voice_chat, inputs=[mic, chat_history, session_id],
|
| 95 |
+
outputs=[audio_out, chat_history, session_id, chatbox])
|
| 96 |
+
|
| 97 |
+
download_btn.click(download_pdf, inputs=[chat_history, session_id], outputs=[download_link])
|
| 98 |
+
|
| 99 |
+
demo.launch()
|