Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import io
|
| 3 |
+
import datetime
|
| 4 |
+
import speech_recognition as sr
|
| 5 |
+
import google.generativeai as genai
|
| 6 |
+
from fastapi import FastAPI, UploadFile, Form, Request
|
| 7 |
+
from fastapi.responses import HTMLResponse, FileResponse
|
| 8 |
+
from fastapi.templating import Jinja2Templates
|
| 9 |
+
from fastapi.staticfiles import StaticFiles
|
| 10 |
+
|
| 11 |
+
# Setup
|
| 12 |
+
app = FastAPI()
|
| 13 |
+
templates = Jinja2Templates(directory="templates")
|
| 14 |
+
|
| 15 |
+
# Gemini API Key (set in environment variable)
|
| 16 |
+
genai.configure(api_key=os.getenv("API"))
|
| 17 |
+
|
| 18 |
+
# Store chats and system prompt
|
| 19 |
+
CHAT_HISTORY = []
|
| 20 |
+
SYSTEM_PROMPT = "You are a helpful AI assistant."
|
| 21 |
+
|
| 22 |
+
@app.get("/", response_class=HTMLResponse)
|
| 23 |
+
async def home(request: Request):
|
| 24 |
+
return templates.TemplateResponse("index.html", {
|
| 25 |
+
"request": request,
|
| 26 |
+
"system_prompt": SYSTEM_PROMPT,
|
| 27 |
+
"chat_history": CHAT_HISTORY
|
| 28 |
+
})
|
| 29 |
+
|
| 30 |
+
@app.post("/upload")
|
| 31 |
+
async def upload_audio(file: UploadFile, system_prompt: str = Form(None)):
|
| 32 |
+
global SYSTEM_PROMPT
|
| 33 |
+
|
| 34 |
+
if system_prompt:
|
| 35 |
+
SYSTEM_PROMPT = system_prompt
|
| 36 |
+
|
| 37 |
+
# Convert audio to text
|
| 38 |
+
recognizer = sr.Recognizer()
|
| 39 |
+
audio_bytes = await file.read()
|
| 40 |
+
with sr.AudioFile(io.BytesIO(audio_bytes)) as source:
|
| 41 |
+
audio_data = recognizer.record(source)
|
| 42 |
+
try:
|
| 43 |
+
extracted_text = recognizer.recognize_google(audio_data)
|
| 44 |
+
except sr.UnknownValueError:
|
| 45 |
+
extracted_text = "[Unclear speech]"
|
| 46 |
+
except sr.RequestError:
|
| 47 |
+
extracted_text = "[Speech recognition service error]"
|
| 48 |
+
|
| 49 |
+
# Send to Gemini
|
| 50 |
+
model = genai.GenerativeModel("gemini-2.5-flash")
|
| 51 |
+
chat_input = f"{SYSTEM_PROMPT}\nUser said: {extracted_text}"
|
| 52 |
+
response = model.generate_content(chat_input)
|
| 53 |
+
|
| 54 |
+
reply = response.text if response and hasattr(response, "text") else "[No response]"
|
| 55 |
+
|
| 56 |
+
# Save chat
|
| 57 |
+
CHAT_HISTORY.append({
|
| 58 |
+
"time": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
| 59 |
+
"user": extracted_text,
|
| 60 |
+
"assistant": reply
|
| 61 |
+
})
|
| 62 |
+
|
| 63 |
+
return {"user_text": extracted_text, "response": reply}
|
| 64 |
+
|
| 65 |
+
@app.get("/download_chats")
|
| 66 |
+
async def download_chats():
|
| 67 |
+
filename = "chat_history.txt"
|
| 68 |
+
with open(filename, "w", encoding="utf-8") as f:
|
| 69 |
+
for chat in CHAT_HISTORY:
|
| 70 |
+
f.write(f"[{chat['time']}]\nUser: {chat['user']}\nAssistant: {chat['assistant']}\n\n")
|
| 71 |
+
return FileResponse(filename, media_type="text/plain", filename=filename)
|