Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from faster_whisper import WhisperModel
|
| 3 |
+
from llama_cpp import Llama
|
| 4 |
+
from brave import Brave
|
| 5 |
+
import os
|
| 6 |
+
import time
|
| 7 |
+
|
| 8 |
+
# Initialize models
|
| 9 |
+
print("Loading models...")
|
| 10 |
+
whisper_model = WhisperModel("tiny", device="cpu", compute_type="int8")
|
| 11 |
+
llm = Llama.from_pretrained(
|
| 12 |
+
repo_id="Qwen/Qwen2.5-0.5B-Instruct-GGUF",
|
| 13 |
+
filename="qwen2.5-0.5b-instruct-q4_k_m.gguf",
|
| 14 |
+
n_ctx=2048,
|
| 15 |
+
n_threads=4,
|
| 16 |
+
verbose=False
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
# Initialize Brave Search
|
| 20 |
+
brave_client = Brave(api_key=os.getenv("BRAVE_API_KEY", ""))
|
| 21 |
+
|
| 22 |
+
def search_web(query, max_results=3):
|
| 23 |
+
"""Perform web search using Brave API"""
|
| 24 |
+
try:
|
| 25 |
+
results = brave_client.search(q=query, count=max_results)
|
| 26 |
+
web_results = results.web_results if hasattr(results, 'web_results') else []
|
| 27 |
+
|
| 28 |
+
context = ""
|
| 29 |
+
for i, result in enumerate(web_results[:max_results], 1):
|
| 30 |
+
context += f"\n[{i}] {result.title}\n{result.description}\n"
|
| 31 |
+
return context.strip()
|
| 32 |
+
except Exception as e:
|
| 33 |
+
return f"Search failed: {str(e)}"
|
| 34 |
+
|
| 35 |
+
def process_audio(audio_path, question_text=None):
|
| 36 |
+
"""Main pipeline: audio -> text -> search -> answer"""
|
| 37 |
+
start_time = time.time()
|
| 38 |
+
|
| 39 |
+
# Step 1: Transcribe audio if provided
|
| 40 |
+
if audio_path:
|
| 41 |
+
segments, _ = whisper_model.transcribe(audio_path, language="en")
|
| 42 |
+
question = " ".join([seg.text for seg in segments])
|
| 43 |
+
else:
|
| 44 |
+
question = question_text
|
| 45 |
+
|
| 46 |
+
if not question:
|
| 47 |
+
return "No input provided", 0.0
|
| 48 |
+
|
| 49 |
+
transcription_time = time.time() - start_time
|
| 50 |
+
|
| 51 |
+
# Step 2: Web search for political/current info
|
| 52 |
+
search_start = time.time()
|
| 53 |
+
search_results = search_web(question)
|
| 54 |
+
search_time = time.time() - search_start
|
| 55 |
+
|
| 56 |
+
# Step 3: Generate answer with LLM
|
| 57 |
+
llm_start = time.time()
|
| 58 |
+
prompt = f"""You are a helpful assistant. Answer the question based on the context below.
|
| 59 |
+
|
| 60 |
+
Context from web search:
|
| 61 |
+
{search_results}
|
| 62 |
+
|
| 63 |
+
Question: {question}
|
| 64 |
+
|
| 65 |
+
Answer briefly and accurately:"""
|
| 66 |
+
|
| 67 |
+
response = llm(
|
| 68 |
+
prompt,
|
| 69 |
+
max_tokens=150,
|
| 70 |
+
temperature=0.3,
|
| 71 |
+
top_p=0.9,
|
| 72 |
+
stop=["Question:", "\n\n"],
|
| 73 |
+
echo=False
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
answer = response['choices'][0]['text'].strip()
|
| 77 |
+
llm_time = time.time() - llm_start
|
| 78 |
+
|
| 79 |
+
total_time = time.time() - start_time
|
| 80 |
+
|
| 81 |
+
timing_info = f"\n\n⏱️ Timing: Transcription={transcription_time:.2f}s | Search={search_time:.2f}s | LLM={llm_time:.2f}s | Total={total_time:.2f}s"
|
| 82 |
+
|
| 83 |
+
return answer + timing_info, total_time
|
| 84 |
+
|
| 85 |
+
# Create Gradio interface
|
| 86 |
+
with gr.Blocks(title="Fast Q&A with Web Search") as demo:
|
| 87 |
+
gr.Markdown("# 🎤 Fast Political Q&A System\nAsk questions via audio or text. Answers in ~3 seconds!")
|
| 88 |
+
|
| 89 |
+
with gr.Tab("Audio Input"):
|
| 90 |
+
audio_input = gr.Audio(type="filepath", label="Record or upload audio question")
|
| 91 |
+
audio_submit = gr.Button("Submit Audio", variant="primary")
|
| 92 |
+
audio_output = gr.Textbox(label="Answer", lines=6)
|
| 93 |
+
audio_time = gr.Number(label="Response Time (seconds)")
|
| 94 |
+
|
| 95 |
+
audio_submit.click(
|
| 96 |
+
fn=lambda x: process_audio(x, None),
|
| 97 |
+
inputs=[audio_input],
|
| 98 |
+
outputs=[audio_output, audio_time],
|
| 99 |
+
api_name="audio_query"
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
with gr.Tab("Text Input"):
|
| 103 |
+
text_input = gr.Textbox(label="Type your question", placeholder="Who won the 2024 elections?")
|
| 104 |
+
text_submit = gr.Button("Submit Text", variant="primary")
|
| 105 |
+
text_output = gr.Textbox(label="Answer", lines=6)
|
| 106 |
+
text_time = gr.Number(label="Response Time (seconds)")
|
| 107 |
+
|
| 108 |
+
text_submit.click(
|
| 109 |
+
fn=lambda x: process_audio(None, x),
|
| 110 |
+
inputs=[text_input],
|
| 111 |
+
outputs=[text_output, text_time],
|
| 112 |
+
api_name="text_query"
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
gr.Markdown("""
|
| 116 |
+
### 📡 API Usage
|
| 117 |
+
```
|
| 118 |
+
# Upload audio file
|
| 119 |
+
curl -F "files=@audio.mp3" https://YOUR-SPACE-URL/upload
|
| 120 |
+
|
| 121 |
+
# Make query
|
| 122 |
+
curl -X POST https://YOUR-SPACE-URL/call/audio_query \\
|
| 123 |
+
-H "Content-Type: application/json" \\
|
| 124 |
+
-d '{"data": [{"path": "/tmp/uploaded_audio.mp3"}]}'
|
| 125 |
+
```
|
| 126 |
+
""")
|
| 127 |
+
|
| 128 |
+
if __name__ == "__main__":
|
| 129 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|