ArchCoder commited on
Commit
d6e4129
·
verified ·
1 Parent(s): d3ea1d4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -58
app.py CHANGED
@@ -1,8 +1,7 @@
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
@@ -18,43 +17,26 @@ llm = Llama.from_pretrained(
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
 
@@ -68,10 +50,10 @@ def process_audio(audio_path, question_text=None):
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
 
@@ -82,48 +64,51 @@ def process_audio(audio_path, question_text=None):
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"):
@@ -180,39 +165,44 @@ with gr.Blocks(title="Fast Q&A with Web Search", theme=gr.themes.Soft()) as demo
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()
 
1
  import gradio as gr
2
  from faster_whisper import WhisperModel
3
  from llama_cpp import Llama
4
+ from duckduckgo_search import DDGS
 
5
  import time
6
 
7
  # Initialize models
 
17
  verbose=False
18
  )
19
 
20
+ # Initialize DuckDuckGo Search (no API key needed!)
21
+ ddgs = DDGS(timeout=3)
22
 
23
  def search_web(query, max_results=3):
24
+ """Perform web search using DuckDuckGo (FREE & UNLIMITED)"""
 
 
 
25
  try:
26
+ # Use text search for fast results
27
+ results = ddgs.text(
28
+ keywords=query,
29
+ region='wt-wt', # Worldwide results
30
+ safesearch='moderate',
31
+ timelimit='m', # Last month for freshness
32
+ max_results=max_results
 
 
 
 
 
 
 
 
33
  )
34
 
 
 
 
 
 
 
35
  context = ""
36
  for i, result in enumerate(results[:max_results], 1):
37
+ title = result.get('title', '')
38
+ body = result.get('body', '')
39
+ context += f"\n[{i}] {title}\n{body}\n"
40
 
41
  return context.strip() if context else "No search results found."
42
 
 
50
  # Step 1: Transcribe audio if provided
51
  if audio_path:
52
  try:
53
+ segments, _ = whisper_model.transcribe(audio_path, language="en", beam_size=1)
54
  question = " ".join([seg.text for seg in segments])
55
  except Exception as e:
56
+ return f"Transcription error: {str(e)}", 0.0
57
  else:
58
  question = question_text
59
 
 
64
 
65
  # Step 2: Web search for current info
66
  search_start = time.time()
67
+ search_results = search_web(question, max_results=2) # Reduced to 2 for speed
68
  search_time = time.time() - search_start
69
 
70
  # Step 3: Generate answer with LLM
71
  llm_start = time.time()
72
+ prompt = f"""Answer the question briefly using the context below.
73
 
74
+ Context:
75
  {search_results}
76
 
77
  Question: {question}
78
 
79
+ Answer:"""
80
 
81
  try:
82
  response = llm(
83
  prompt,
84
+ max_tokens=120, # Reduced for faster generation
85
+ temperature=0.2, # Lower for faster, more focused responses
86
+ top_p=0.85,
87
  stop=["Question:", "\n\n\n"],
88
  echo=False
89
  )
90
 
91
  answer = response['choices'][0]['text'].strip()
92
  except Exception as e:
93
+ answer = f"LLM error: {str(e)}"
94
 
95
  llm_time = time.time() - llm_start
96
  total_time = time.time() - start_time
97
 
98
+ # Color code timing (green if under 3s, yellow if close, red if over)
99
+ time_emoji = "🟢" if total_time < 3.0 else "🟡" if total_time < 3.5 else "🔴"
100
+
101
+ timing_info = f"\n\n{time_emoji} **Timing:** Trans={transcription_time:.2f}s | Search={search_time:.2f}s | LLM={llm_time:.2f}s | **Total={total_time:.2f}s**"
102
 
103
  return answer + timing_info, total_time
104
 
105
  # Create Gradio interface
106
+ with gr.Blocks(title="Fast Q&A - FREE Unlimited Search", theme=gr.themes.Soft()) as demo:
107
  gr.Markdown("""
108
+ # Ultra-Fast Political Q&A System
109
+ Ask questions via audio or text. **FREE unlimited web search** with DuckDuckGo!
110
 
111
+ **Features:** Whisper-tiny + Qwen2.5-0.5B + DuckDuckGo (No API Key!)
112
  """)
113
 
114
  with gr.Tab("🎙️ Audio Input"):
 
165
  examples=[
166
  ["Who won the 2024 US presidential election?"],
167
  ["What is the current inflation rate in India?"],
168
+ ["Who is the prime minister of UK?"],
169
+ ["What is the latest news about AI?"]
170
  ],
171
  inputs=text_input
172
  )
173
 
174
+ with gr.Accordion("📡 API Usage via curl", open=False):
175
  gr.Markdown("""
176
+ ### Text Query (Simplest):
 
 
177
  ```
178
  curl -X POST https://archcoder-basic-app.hf.space/call/text_query \\
179
  -H "Content-Type: application/json" \\
180
  -d '{"data": ["Who is the current US president?"]}'
181
  ```
182
 
183
+ ### Audio Query:
184
  ```
185
+ # Upload audio
186
  curl -F "files=@audio.mp3" https://archcoder-basic-app.hf.space/upload
187
 
188
+ # Query (replace path from upload response)
189
  curl -X POST https://archcoder-basic-app.hf.space/call/audio_query \\
190
  -H "Content-Type: application/json" \\
191
+ -d '{"data": [{"path": "/tmp/gradio/YOUR_FILE.mp3"}]}'
192
  ```
193
  """)
194
 
195
  gr.Markdown("""
196
  ---
197
+ ### 🎯 System Specs
198
+ - **Search:** DuckDuckGo (FREE, unlimited, no API key!)
199
+ - **Transcription:** Whisper-tiny (optimized for speed)
200
+ - **LLM:** Qwen2.5-0.5B Q4 (fast factual answers)
201
+ - **Target:** Sub-3s total response time
202
+
203
+ 🟢 = Under 3s | 🟡 = 3-3.5s | 🔴 = Over 3.5s
204
  """)
205
 
206
  if __name__ == "__main__":
207
+ demo.queue(max_size=5) # Limit queue for consistent performance
208
  demo.launch()