Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,15 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from faster_whisper import WhisperModel
|
| 3 |
from llama_cpp import Llama
|
| 4 |
-
|
| 5 |
import os
|
| 6 |
import time
|
| 7 |
|
| 8 |
# Initialize models
|
| 9 |
-
print("Loading
|
| 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",
|
|
@@ -16,19 +18,46 @@ llm = Llama.from_pretrained(
|
|
| 16 |
verbose=False
|
| 17 |
)
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
|
| 22 |
def search_web(query, max_results=3):
|
| 23 |
"""Perform web search using Brave API"""
|
|
|
|
|
|
|
|
|
|
| 24 |
try:
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
context = ""
|
| 29 |
-
for i, result in enumerate(
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
except Exception as e:
|
| 33 |
return f"Search failed: {str(e)}"
|
| 34 |
|
|
@@ -38,59 +67,82 @@ def process_audio(audio_path, question_text=None):
|
|
| 38 |
|
| 39 |
# Step 1: Transcribe audio if provided
|
| 40 |
if audio_path:
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
| 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
|
| 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
|
| 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
|
| 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("
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
audio_submit.click(
|
| 96 |
fn=lambda x: process_audio(x, None),
|
|
@@ -99,11 +151,23 @@ with gr.Blocks(title="Fast Q&A with Web Search") as demo:
|
|
| 99 |
api_name="audio_query"
|
| 100 |
)
|
| 101 |
|
| 102 |
-
with gr.Tab("Text Input"):
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
text_submit.click(
|
| 109 |
fn=lambda x: process_audio(None, x),
|
|
@@ -111,19 +175,44 @@ with gr.Blocks(title="Fast Q&A with Web Search") as demo:
|
|
| 111 |
outputs=[text_output, text_time],
|
| 112 |
api_name="text_query"
|
| 113 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
gr.Markdown("""
|
| 116 |
-
|
| 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.
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from faster_whisper import WhisperModel
|
| 3 |
from llama_cpp import Llama
|
| 4 |
+
import requests
|
| 5 |
import os
|
| 6 |
import time
|
| 7 |
|
| 8 |
# Initialize models
|
| 9 |
+
print("Loading Whisper model...")
|
| 10 |
whisper_model = WhisperModel("tiny", device="cpu", compute_type="int8")
|
| 11 |
+
|
| 12 |
+
print("Loading LLM...")
|
| 13 |
llm = Llama.from_pretrained(
|
| 14 |
repo_id="Qwen/Qwen2.5-0.5B-Instruct-GGUF",
|
| 15 |
filename="qwen2.5-0.5b-instruct-q4_k_m.gguf",
|
|
|
|
| 18 |
verbose=False
|
| 19 |
)
|
| 20 |
|
| 21 |
+
# Get Brave API key from environment
|
| 22 |
+
BRAVE_API_KEY = os.getenv("BRAVE_API_KEY", "")
|
| 23 |
|
| 24 |
def search_web(query, max_results=3):
|
| 25 |
"""Perform web search using Brave API"""
|
| 26 |
+
if not BRAVE_API_KEY:
|
| 27 |
+
return "⚠️ Brave API key not configured. Add it in Space Settings."
|
| 28 |
+
|
| 29 |
try:
|
| 30 |
+
headers = {
|
| 31 |
+
"Accept": "application/json",
|
| 32 |
+
"Accept-Encoding": "gzip",
|
| 33 |
+
"X-Subscription-Token": BRAVE_API_KEY
|
| 34 |
+
}
|
| 35 |
+
params = {
|
| 36 |
+
"q": query,
|
| 37 |
+
"count": max_results
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
response = requests.get(
|
| 41 |
+
"https://api.search.brave.com/res/v1/web/search",
|
| 42 |
+
headers=headers,
|
| 43 |
+
params=params,
|
| 44 |
+
timeout=2
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
if response.status_code != 200:
|
| 48 |
+
return f"Search error: {response.status_code}"
|
| 49 |
+
|
| 50 |
+
data = response.json()
|
| 51 |
+
results = data.get("web", {}).get("results", [])
|
| 52 |
|
| 53 |
context = ""
|
| 54 |
+
for i, result in enumerate(results[:max_results], 1):
|
| 55 |
+
title = result.get("title", "")
|
| 56 |
+
description = result.get("description", "")
|
| 57 |
+
context += f"\n[{i}] {title}\n{description}\n"
|
| 58 |
+
|
| 59 |
+
return context.strip() if context else "No search results found."
|
| 60 |
+
|
| 61 |
except Exception as e:
|
| 62 |
return f"Search failed: {str(e)}"
|
| 63 |
|
|
|
|
| 67 |
|
| 68 |
# Step 1: Transcribe audio if provided
|
| 69 |
if audio_path:
|
| 70 |
+
try:
|
| 71 |
+
segments, _ = whisper_model.transcribe(audio_path, language="en")
|
| 72 |
+
question = " ".join([seg.text for seg in segments])
|
| 73 |
+
except Exception as e:
|
| 74 |
+
return f"Transcription error: {str(e)}", 0.0
|
| 75 |
else:
|
| 76 |
question = question_text
|
| 77 |
|
| 78 |
+
if not question or question.strip() == "":
|
| 79 |
+
return "❌ No input provided", 0.0
|
| 80 |
|
| 81 |
transcription_time = time.time() - start_time
|
| 82 |
|
| 83 |
+
# Step 2: Web search for current info
|
| 84 |
search_start = time.time()
|
| 85 |
search_results = search_web(question)
|
| 86 |
search_time = time.time() - search_start
|
| 87 |
|
| 88 |
# Step 3: Generate answer with LLM
|
| 89 |
llm_start = time.time()
|
| 90 |
+
prompt = f"""You are a helpful assistant. Answer the question briefly based on the context below.
|
| 91 |
|
| 92 |
Context from web search:
|
| 93 |
{search_results}
|
| 94 |
|
| 95 |
Question: {question}
|
| 96 |
|
| 97 |
+
Answer (be concise and accurate):"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
+
try:
|
| 100 |
+
response = llm(
|
| 101 |
+
prompt,
|
| 102 |
+
max_tokens=150,
|
| 103 |
+
temperature=0.3,
|
| 104 |
+
top_p=0.9,
|
| 105 |
+
stop=["Question:", "\n\n\n"],
|
| 106 |
+
echo=False
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
answer = response['choices'][0]['text'].strip()
|
| 110 |
+
except Exception as e:
|
| 111 |
+
answer = f"LLM error: {str(e)}"
|
| 112 |
+
|
| 113 |
+
llm_time = time.time() - llm_start
|
| 114 |
total_time = time.time() - start_time
|
| 115 |
|
| 116 |
+
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**"
|
| 117 |
|
| 118 |
return answer + timing_info, total_time
|
| 119 |
|
| 120 |
# Create Gradio interface
|
| 121 |
+
with gr.Blocks(title="Fast Q&A with Web Search", theme=gr.themes.Soft()) as demo:
|
| 122 |
+
gr.Markdown("""
|
| 123 |
+
# 🎤 Fast Political Q&A System
|
| 124 |
+
Ask questions via audio or text. Get web-grounded answers in ~3 seconds!
|
| 125 |
+
|
| 126 |
+
**Features:** Whisper-tiny + Qwen2.5-0.5B + Brave Search API
|
| 127 |
+
""")
|
| 128 |
+
|
| 129 |
+
with gr.Tab("🎙️ Audio Input"):
|
| 130 |
+
with gr.Row():
|
| 131 |
+
with gr.Column():
|
| 132 |
+
audio_input = gr.Audio(
|
| 133 |
+
sources=["microphone", "upload"],
|
| 134 |
+
type="filepath",
|
| 135 |
+
label="Record or upload audio"
|
| 136 |
+
)
|
| 137 |
+
audio_submit = gr.Button("🚀 Submit Audio", variant="primary", size="lg")
|
| 138 |
+
|
| 139 |
+
with gr.Column():
|
| 140 |
+
audio_output = gr.Textbox(
|
| 141 |
+
label="Answer",
|
| 142 |
+
lines=8,
|
| 143 |
+
show_copy_button=True
|
| 144 |
+
)
|
| 145 |
+
audio_time = gr.Number(label="Response Time (seconds)", precision=2)
|
| 146 |
|
| 147 |
audio_submit.click(
|
| 148 |
fn=lambda x: process_audio(x, None),
|
|
|
|
| 151 |
api_name="audio_query"
|
| 152 |
)
|
| 153 |
|
| 154 |
+
with gr.Tab("✍️ Text Input"):
|
| 155 |
+
with gr.Row():
|
| 156 |
+
with gr.Column():
|
| 157 |
+
text_input = gr.Textbox(
|
| 158 |
+
label="Type your question",
|
| 159 |
+
placeholder="Who is the current US president?",
|
| 160 |
+
lines=3
|
| 161 |
+
)
|
| 162 |
+
text_submit = gr.Button("🚀 Submit Text", variant="primary", size="lg")
|
| 163 |
+
|
| 164 |
+
with gr.Column():
|
| 165 |
+
text_output = gr.Textbox(
|
| 166 |
+
label="Answer",
|
| 167 |
+
lines=8,
|
| 168 |
+
show_copy_button=True
|
| 169 |
+
)
|
| 170 |
+
text_time = gr.Number(label="Response Time (seconds)", precision=2)
|
| 171 |
|
| 172 |
text_submit.click(
|
| 173 |
fn=lambda x: process_audio(None, x),
|
|
|
|
| 175 |
outputs=[text_output, text_time],
|
| 176 |
api_name="text_query"
|
| 177 |
)
|
| 178 |
+
|
| 179 |
+
gr.Examples(
|
| 180 |
+
examples=[
|
| 181 |
+
["Who won the 2024 US presidential election?"],
|
| 182 |
+
["What is the current inflation rate in India?"],
|
| 183 |
+
["Who is the prime minister of UK?"]
|
| 184 |
+
],
|
| 185 |
+
inputs=text_input
|
| 186 |
+
)
|
| 187 |
+
|
| 188 |
+
with gr.Accordion("📡 API Usage", open=False):
|
| 189 |
+
gr.Markdown("""
|
| 190 |
+
### Using curl to query this endpoint:
|
| 191 |
+
|
| 192 |
+
**Text Query:**
|
| 193 |
+
```
|
| 194 |
+
curl -X POST https://archcoder-basic-app.hf.space/call/text_query \\
|
| 195 |
+
-H "Content-Type: application/json" \\
|
| 196 |
+
-d '{"data": ["Who is the current US president?"]}'
|
| 197 |
+
```
|
| 198 |
+
|
| 199 |
+
**Audio Query:**
|
| 200 |
+
```
|
| 201 |
+
# 1. Upload audio file
|
| 202 |
+
curl -F "files=@audio.mp3" https://archcoder-basic-app.hf.space/upload
|
| 203 |
+
|
| 204 |
+
# 2. Query with returned path
|
| 205 |
+
curl -X POST https://archcoder-basic-app.hf.space/call/audio_query \\
|
| 206 |
+
-H "Content-Type: application/json" \\
|
| 207 |
+
-d '{"data": [{"path": "/tmp/gradio/audio.mp3"}]}'
|
| 208 |
+
```
|
| 209 |
+
""")
|
| 210 |
|
| 211 |
gr.Markdown("""
|
| 212 |
+
---
|
| 213 |
+
**Note:** This Space uses free-tier resources. For production use, consider upgrading to a persistent Space.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
""")
|
| 215 |
|
| 216 |
if __name__ == "__main__":
|
| 217 |
+
demo.queue()
|
| 218 |
+
demo.launch()
|