Spaces:
Running
on
Zero
Running
on
Zero
File size: 17,170 Bytes
cf40b67 835fc41 cf40b67 835fc41 b17a402 835fc41 b17a402 835fc41 b17a402 835fc41 b17a402 fff1212 7625ecd 835fc41 cf40b67 b17a402 835fc41 b17a402 f216857 b17a402 835fc41 b17a402 cf40b67 835fc41 cf40b67 b17a402 cf40b67 b17a402 835fc41 cf40b67 b17a402 cf40b67 b17a402 cf40b67 835fc41 b17a402 835fc41 b17a402 835fc41 b17a402 835fc41 e109618 f216857 b17a402 8a9a6c3 f216857 b17a402 835fc41 b17a402 f216857 835fc41 f216857 835fc41 b17a402 835fc41 f216857 b17a402 835fc41 b17a402 835fc41 f216857 b17a402 835fc41 b17a402 835fc41 b17a402 835fc41 b17a402 8a9a6c3 835fc41 fff1212 c908926 835fc41 cf40b67 b17a402 cf40b67 b17a402 835fc41 fff1212 c908926 b17a402 835fc41 cf40b67 835fc41 b17a402 f216857 b17a402 835fc41 c908926 835fc41 f216857 835fc41 f216857 835fc41 f216857 835fc41 b17a402 c908926 b17a402 8a9a6c3 835fc41 f216857 c908926 cf40b67 835fc41 cf40b67 835fc41 cf40b67 835fc41 cf40b67 835fc41 cf40b67 8366798 cf40b67 c5f69a5 cf40b67 835fc41 cf40b67 835fc41 cf40b67 f216857 |
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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 |
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer
import spaces
from duckduckgo_search import DDGS
import time
import torch
from datetime import datetime
import os
import subprocess
import numpy as np
from typing import List, Dict, Tuple, Any
# Install required dependencies for Kokoro with better error handling
try:
subprocess.run(['git', 'lfs', 'install'], check=True)
if not os.path.exists('Kokoro-82M'):
subprocess.run(['git', 'clone', 'https://huggingface.co/hexgrad/Kokoro-82M'], check=True)
# Try installing espeak with proper package manager commands
try:
subprocess.run(['apt-get', 'update'], check=True)
subprocess.run(['apt-get', 'install', '-y', 'espeak'], check=True)
except subprocess.CalledProcessError:
print("Warning: Could not install espeak. Attempting espeak-ng...")
try:
subprocess.run(['apt-get', 'install', '-y', 'espeak-ng'], check=True)
except subprocess.CalledProcessError:
print("Warning: Could not install espeak or espeak-ng. TTS functionality may be limited.")
except Exception as e:
print(f"Warning: Initial setup error: {str(e)}")
print("Continuing with limited functionality...")
# --- Initialization (Do this ONCE) ---
model_name = "deepseek-ai/DeepSeek-R1-Distill-Llama-8B"
tokenizer = AutoTokenizer.from_pretrained(model_name)
tokenizer.pad_token = tokenizer.eos_token
# Initialize DeepSeek model
model = AutoModelForCausalLM.from_pretrained(
model_name,
device_map="auto",
offload_folder="offload",
low_cpu_mem_usage=True,
torch_dtype=torch.float16
)
# Initialize Kokoro TTS (with error handling)
VOICE_CHOICES = {
'๐บ๐ธ Female (Default)': 'af',
'๐บ๐ธ Bella': 'af_bella',
'๐บ๐ธ Sarah': 'af_sarah',
'๐บ๐ธ Nicole': 'af_nicole'
}
TTS_ENABLED = False
TTS_MODEL = None
VOICEPACK = None
try:
if os.path.exists('Kokoro-82M'):
import sys
sys.path.append('Kokoro-82M')
from models import build_model # type: ignore
from kokoro import generate # type: ignore
device = 'cuda' if torch.cuda.is_available() else 'cpu'
TTS_MODEL = build_model('Kokoro-82M/kokoro-v0_19.pth', device)
# Load default voice
try:
VOICEPACK = torch.load('Kokoro-82M/voices/af.pt', map_location=device, weights_only=True)
except Exception as e:
print(f"Warning: Could not load default voice: {e}")
raise
TTS_ENABLED = True
else:
print("Warning: Kokoro-82M directory not found. TTS disabled.")
except Exception as e:
print(f"Warning: Could not initialize Kokoro TTS: {str(e)}")
TTS_ENABLED = False
def get_web_results(query: str, max_results: int = 5) -> List[Dict[str, str]]:
"""Get web search results using DuckDuckGo"""
try:
with DDGS() as ddgs:
results = list(ddgs.text(query, max_results=max_results))
return [{
"title": result.get("title", ""),
"snippet": result["body"],
"url": result["href"],
"date": result.get("published", "")
} for result in results]
except Exception as e:
print(f"Error in web search: {e}")
return []
def format_prompt(query: str, context: List[Dict[str, str]]) -> str:
"""Format the prompt with web context"""
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
context_lines = '\n'.join([f'- [{res["title"]}]: {res["snippet"]}' for res in context])
return f"""You are an intelligent search assistant. Answer the user's query using the provided web context.
Current Time: {current_time}
Important: For election-related queries, please distinguish clearly between different election years and types (presidential vs. non-presidential). Only use information from the provided web context.
Query: {query}
Web Context:
{context_lines}
Provide a detailed answer in markdown format. Include relevant information from sources and cite them using [1], [2], etc. If the query is about elections, clearly specify which year and type of election you're discussing.
Answer:"""
def format_sources(web_results: List[Dict[str, str]]) -> str:
"""Format sources with more details"""
if not web_results:
return "<div class='no-sources'>No sources available</div>"
sources_html = "<div class='sources-container'>"
for i, res in enumerate(web_results, 1):
title = res["title"] or "Source"
date = f"<span class='source-date'>{res['date']}</span>" if res['date'] else ""
sources_html += f"""
<div class='source-item'>
<div class='source-number'>[{i}]</div>
<div class='source-content'>
<a href="{res['url']}" target="_blank" class='source-title'>{title}</a>
{date}
<div class='source-snippet'>{res['snippet'][:150]}...</div>
</div>
</div>
"""
sources_html += "</div>"
return sources_html
@spaces.GPU(duration=30)
def generate_answer(prompt: str) -> str:
"""Generate answer using the DeepSeek model"""
inputs = tokenizer(
prompt,
return_tensors="pt",
padding=True,
truncation=True,
max_length=512,
return_attention_mask=True
).to(model.device)
outputs = model.generate(
inputs.input_ids,
attention_mask=inputs.attention_mask,
max_new_tokens=256,
temperature=0.7,
top_p=0.95,
pad_token_id=tokenizer.eos_token_id,
do_sample=True,
early_stopping=True
)
return tokenizer.decode(outputs[0], skip_special_tokens=True)
@spaces.GPU(duration=30)
def generate_speech_with_gpu(text: str, voice_name: str = 'af', tts_model=TTS_MODEL, voicepack=VOICEPACK) -> Tuple[int, np.ndarray] | None:
"""Generate speech from text using Kokoro TTS model."""
if not TTS_ENABLED or tts_model is None:
print("TTS is not enabled or model is not loaded.")
return None
try:
device = 'cuda' if torch.cuda.is_available() else 'cpu'
# Handle voicepack loading
voice_file = f'Kokoro-82M/voices/{voice_name}.pt'
if voice_name == 'af' and voicepack is not None:
# Use the pre-loaded default voicepack
pass
elif os.path.exists(voice_file):
# Load the selected voicepack if it exists
voicepack = torch.load(voice_file, map_location=device, weights_only=True)
else:
# Fall back to default 'af' if selected voicepack is missing
print(f"Voicepack {voice_name}.pt not found. Falling back to default 'af'.")
voice_file = 'Kokoro-82M/voices/af.pt'
if os.path.exists(voice_file):
voicepack = torch.load(voice_file, map_location=device, weights_only=True)
else:
print("Default voicepack 'af.pt' not found. Cannot generate audio.")
return None
# Clean the text
clean_text = ' '.join([line for line in text.split('\n') if not line.startswith('#')])
clean_text = clean_text.replace('[', '').replace(']', '').replace('*', '')
# Split long text into chunks
max_chars = 1000
chunks = []
if len(clean_text) > max_chars:
sentences = clean_text.split('.')
current_chunk = ""
for sentence in sentences:
if len(current_chunk) + len(sentence) + 1 < max_chars:
current_chunk += sentence + "."
else:
chunks.append(current_chunk.strip())
current_chunk = sentence + "."
if current_chunk:
chunks.append(current_chunk.strip())
else:
chunks = [clean_text]
# Generate audio for each chunk
audio_chunks = []
for chunk in chunks:
if chunk.strip():
chunk_audio, _ = generate(tts_model, chunk, voicepack, lang='a')
if isinstance(chunk_audio, torch.Tensor):
chunk_audio = chunk_audio.cpu().numpy()
audio_chunks.append(chunk_audio)
# Concatenate chunks
if audio_chunks:
final_audio = np.concatenate(audio_chunks) if len(audio_chunks) > 1 else audio_chunks[0]
return (24000, final_audio)
else:
return None
except Exception as e:
print(f"Error generating speech: {str(e)}")
return None
def process_query(query: str, history: List[List[str]], selected_voice: str = 'af'):
"""Process user query with streaming effect"""
try:
if history is None:
history = []
# Get web results first
web_results = get_web_results(query)
sources_html = format_sources(web_results)
current_history = history + [[query, "*Searching...*"]]
# Yield initial searching state
yield (
"*Searching & Thinking...*", # answer_output (Markdown)
sources_html, # sources_output (HTML)
"Searching...", # search_btn (Button)
current_history, # chat_history_display (Chatbot)
None # audio_output (Audio)
)
# Generate answer
prompt = format_prompt(query, web_results)
answer = generate_answer(prompt)
final_answer = answer.split("Answer:")[-1].strip()
# Update history before TTS
updated_history = history + [[query, final_answer]]
# Generate speech from the answer (only if enabled)
if TTS_ENABLED:
yield (
final_answer, # answer_output
sources_html, # sources_output
"Generating audio...", # search_btn
updated_history, # chat_history_display
None # audio_output
)
try:
audio = generate_speech_with_gpu(final_answer, selected_voice)
if audio is None:
final_answer += "\n\n*Audio generation failed. The voicepack may be missing or incompatible.*"
except Exception as e:
final_answer += f"\n\n*Error generating audio: {str(e)}*"
audio = None
else:
final_answer += "\n\n*TTS is disabled. Audio not available.*"
audio = None
# Yield final result
yield (
final_answer, # answer_output
sources_html, # sources_output
"Search", # search_btn
updated_history, # chat_history_display
audio if audio is not None else None # audio_output
)
except Exception as e:
error_message = str(e)
if "GPU quota" in error_message:
error_message = "โ ๏ธ GPU quota exceeded. Please try again later when the daily quota resets."
yield (
f"Error: {error_message}", # answer_output
sources_html, # sources_output
"Search", # search_btn
history + [[query, f"*Error: {error_message}*"]], # chat_history_display
None # audio_output
)
# Update the CSS for better contrast and readability
css = """
.gradio-container {
max-width: 1200px !important;
background-color: #f7f7f8 !important;
}
#header {
text-align: center;
margin-bottom: 2rem;
padding: 2rem 0;
background: #1a1b1e;
border-radius: 12px;
color: white;
}
#header h1 {
color: white;
font-size: 2.5rem;
margin-bottom: 0.5rem;
}
#header h3 {
color: #a8a9ab;
}
.search-container {
background: #1a1b1e;
border-radius: 12px;
box-shadow: 0 4px 12px rgba(0,0,0,0.1);
padding: 1rem;
margin-bottom: 1rem;
}
.search-box {
padding: 1rem;
background: #2c2d30;
border-radius: 8px;
margin-bottom: 1rem;
}
.search-box input[type="text"] {
background: #3a3b3e !important;
border: 1px solid #4a4b4e !important;
color: white !important;
border-radius: 8px !important;
}
.search-box input[type="text"]::placeholder {
color: #a8a9ab !important;
}
.search-box button {
background: #2563eb !important;
border: none !important;
}
.results-container {
background: #2c2d30;
border-radius: 8px;
padding: 1rem;
margin-top: 1rem;
}
.answer-box {
background: #3a3b3e;
border-radius: 8px;
padding: 1.5rem;
color: white;
margin-bottom: 1rem;
}
.answer-box p {
color: #e5e7eb;
line-height: 1.6;
}
.sources-container {
margin-top: 1rem;
background: #2c2d30;
border-radius: 8px;
padding: 1rem;
}
.source-item {
display: flex;
padding: 12px;
margin: 8px 0;
background: #3a3b3e;
border-radius: 8px;
transition: all 0.2s;
}
.source-item:hover {
background: #4a4b4e;
}
.source-number {
font-weight: bold;
margin-right: 12px;
color: #60a5fa;
}
.source-content {
flex: 1;
}
.source-title {
color: #60a5fa;
font-weight: 500;
text-decoration: none;
display: block;
margin-bottom: 4px;
}
.source-date {
color: #a8a9ab;
font-size: 0.9em;
margin-left: 8px;
}
.source-snippet {
color: #e5e7eb;
font-size: 0.9em;
line-height: 1.4;
}
.chat-history {
max-height: 400px;
overflow-y: auto;
padding: 1rem;
background: #2c2d30;
border-radius: 8px;
margin-top: 1rem;
}
.examples-container {
background: #2c2d30;
border-radius: 8px;
padding: 1rem;
margin-top: 1rem;
}
.examples-container button {
background: #3a3b3e !important;
border: 1px solid #4a4b4e !important;
color: #e5e7eb !important;
}
.markdown-content {
color: #e5e7eb !important;
}
.markdown-content h1, .markdown-content h2, .markdown-content h3 {
color: white !important;
}
.markdown-content a {
color: #60a5fa !important;
}
.accordion {
background: #2c2d30 !important;
border-radius: 8px !important;
margin-top: 1rem !important;
}
.voice-selector {
margin-top: 1rem;
background: #2c2d30;
border-radius: 8px;
padding: 0.5rem;
}
.voice-selector select {
background: #3a3b3e !important;
color: white !important;
border: 1px solid #4a4b4e !important;
}
"""
# Update the Gradio interface layout
with gr.Blocks(title="AI Search Assistant", css=css, theme="dark") as demo:
chat_history = gr.State([])
with gr.Column(elem_id="header"):
gr.Markdown("# ๐ AI Search Assistant")
gr.Markdown("### Powered by DeepSeek & Real-time Web Results with Voice")
with gr.Column(elem_classes="search-container"):
with gr.Row(elem_classes="search-box"):
search_input = gr.Textbox(
label="",
placeholder="Ask anything...",
scale=5,
container=False
)
search_btn = gr.Button("Search", variant="primary", scale=1)
voice_select = gr.Dropdown(
choices=list(VOICE_CHOICES.items()),
value='af',
label="Select Voice",
elem_classes="voice-selector"
)
with gr.Row(elem_classes="results-container"):
with gr.Column(scale=2):
with gr.Column(elem_classes="answer-box"):
answer_output = gr.Markdown(elem_classes="markdown-content")
with gr.Row():
audio_output = gr.Audio(label="Voice Response", elem_classes="audio-player")
with gr.Accordion("Chat History", open=False, elem_classes="accordion"):
chat_history_display = gr.Chatbot(elem_classes="chat-history")
with gr.Column(scale=1):
with gr.Column(elem_classes="sources-box"):
gr.Markdown("### Sources")
sources_output = gr.HTML()
with gr.Row(elem_classes="examples-container"):
gr.Examples(
examples=[
"musk explores blockchain for doge",
"nvidia to launch new gaming card",
"What are the best practices for sustainable living?",
"tesla mistaken for asteroid"
],
inputs=search_input,
label="Try these examples"
)
# Handle interactions
search_btn.click(
fn=process_query,
inputs=[search_input, chat_history, voice_select],
outputs=[answer_output, sources_output, search_btn, chat_history_display, audio_output]
)
# Also trigger search on Enter key
search_input.submit(
fn=process_query,
inputs=[search_input, chat_history, voice_select],
outputs=[answer_output, sources_output, search_btn, chat_history_display, audio_output]
)
if __name__ == "__main__":
demo.launch(share=True) |