-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
104 lines (83 loc) · 2.93 KB
/
main.py
File metadata and controls
104 lines (83 loc) · 2.93 KB
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
from fastapi import FastAPI, File, UploadFile
from fastapi.middleware.cors import CORSMiddleware
from faster_whisper import WhisperModel
from duckduckgo_search import DDGS
from openai import OpenAI
import tempfile
import os
import uuid
app = FastAPI()
# Allow mobile app to call backend
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # dev only
allow_methods=["*"],
allow_headers=["*"],
)
# Load Whisper model once
model = WhisperModel("base", compute_type="int8")
# OpenAI client
client = OpenAI(api_key="YOUR_API_KEY")
# Simple in-memory session storage
sessions = {}
@app.post("/search")
async def search(file: UploadFile = File(...)):
# 1️⃣ Create session
session_id = str(uuid.uuid4())
sessions[session_id] = []
# 2️⃣ Save audio temporarily
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_audio:
audio_path = temp_audio.name
temp_audio.write(await file.read())
try:
# 3️⃣ Speech → Text
segments, info = model.transcribe(audio_path, task="translate")
query = "".join(seg.text for seg in segments).strip()
# 4️⃣ Web search snippets
snippets_text = web_search_snippets(query)
# 5️⃣ Summarize / complete answer with OpenAI GPT-4
answer = summarize_results(query, snippets_text)
# 6️⃣ Save memory
sessions[session_id].append({
"query": query,
"answer": answer
})
return {
"session_id": session_id,
"query": query,
"result": answer,
"memory": sessions[session_id]
}
finally:
# 7️⃣ Delete temp audio
if os.path.exists(audio_path):
os.remove(audio_path)
# 🔎 Web search - return combined snippets (no truncation)
def web_search_snippets(query: str) -> str:
results = []
try:
with DDGS() as ddgs:
for r in ddgs.text(query, max_results=5): # get more results for completeness
snippet = r.get("snippet")
if snippet:
results.append(snippet)
except Exception as e:
print("Web search failed:", e)
return "I couldn't search the web right now."
return " ".join(results) if results else "I couldn't find relevant information."
# 🧠 OpenAI GPT-4 summarization & completion
def summarize_results(query: str, results_text: str) -> str:
prompt = f"""
You are an AI assistant. The following web content may be in any language.
Please **understand it fully**, summarize it concisely, and answer the question **in English**.
Question: {query}
Web Content:
{results_text}
Answer the question completely and clearly in English:"""
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": prompt}],
max_tokens=500,
temperature=0.7
)
return response.choices[0].message.content.strip()