Update app.py
Browse files6 unmodified lines7891011121314151617181920212223242526272829303132338 unmodified lines424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481499 unmodified lines1591601611621631641659 unmodified lines1751761771781791801811821831841851867 unmodified lines1941951961971981992008 unmodified lines2092102112122132142152162172182192202215 unmodified lines2272282292302312322332342352361 unmodified line23823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327421 unmodified lines2962972982993003013023033043053063076 unmodified linesimport base64import tempfileimport osfrom datetime import datetime# Initialize modelsprint("Loading Whisper model...")whisper_model = WhisperModel("tiny", device="cpu", compute_type="int8")print("Loading LLM...")model_name = "Qwen/Qwen2.5-1.5B-Instruct" # Upgraded to 1.5B for better qualitytokenizer = AutoTokenizer.from_pretrained(model_name)model = AutoModelForCausalLM.from_pretrained(model_name,torch_dtype=torch.float32,device_map="cpu",low_cpu_mem_usage=True)# Initialize DuckDuckGo Searchddgs = DDGS(timeout=3)def search_web(query, max_results=3):"""Perform web search using DuckDuckGo"""try:results = ddgs.text(keywords=query,8 unmodified linestitle = result.get('title', '')body = result.get('body', '')context += f"\n[Source {i}] {title}\n{body}\n"return context.strip() if context else "No search results found."except Exception as e:return f"Search failed: {str(e)}"def transcribe_audio_base64(audio_base64):"""Transcribe audio from base64 string (for Pluely STT endpoint)"""try:audio_bytes = base64.b64decode(audio_base64)with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_audio:temp_audio.write(audio_bytes)temp_path = temp_audio.namesegments, _ = whisper_model.transcribe(temp_path, language="en", beam_size=1)transcription = " ".join([seg.text for seg in segments])os.unlink(temp_path)return {"text": transcription.strip()}except Exception as e:return {"error": f"Transcription failed: {str(e)}"}def generate_answer(text_input):"""Generate complete answer with context"""try:if not text_input or text_input.strip() == "":return "No input provided"# Get current date for contextcurrent_date = datetime.now().strftime("%B %d, %Y")# Web search for current informationsearch_results = search_web(text_input, max_results=3)# Enhanced prompt for comprehensive responsesmessages = [{"role": "system", "content": f"""You are a knowledgeable assistant providing comprehensive, well-researched answers. Today's date is {current_date}.When answering:1. Provide the direct answer first2. Add relevant context and background information3. Include recent developments or current status when applicable4. Be informative but concise (150-200 words)5. Use the web search results to ensure accuracy and currency"""},{"role": "user", "content": f"""Based on these current web search results:{search_results}Question: {text_input}Provide a comprehensive answer that includes:- Direct answer to the question- Relevant context and background- Recent developments (as of {current_date})- Key points the user should knowAnswer:"""}]text = tokenizer.apply_chat_template(messages,tokenize=False,add_generation_prompt=True)inputs = tokenizer([text], return_tensors="pt").to("cpu")with torch.no_grad():outputs = model.generate(**inputs,max_new_tokens=250, # Increased from 80 to 250temperature=0.3, # Slightly higher for more natural responsesdo_sample=True,top_p=0.9,repetition_penalty=1.1,pad_token_id=tokenizer.eos_token_id)response = tokenizer.decode(outputs[0][inputs['input_ids'].shape[1]:], skip_special_tokens=True)return response.strip()except Exception as e:return f"Error: {str(e)}"def process_audio(audio_path, question_text):"""Main pipeline - returns tuple (answer, time)"""start_time = time.time()# Transcribe if audio providedif audio_path:try:segments, _ = whisper_model.transcribe(audio_path, language="en", beam_size=1)question = " ".join([seg.text for seg in segments])except Exception as e:return f"❌ Transcription error: {str(e)}", 0.0else:question = question_textif not question or question.strip() == "":return "❌ No input provided", 0.0transcription_time = time.time() - start_time9 unmodified linesllm_time = time.time() - llm_starttotal_time = time.time() - start_timetime_emoji = "🟢" if total_time < 5.0 else "🟡" if total_time < 7.0 else "🔴"timing_info = f"\n\n{time_emoji} **Performance:** Trans={transcription_time:.2f}s | Search={search_time:.2f}s | LLM={llm_time:.2f}s | **Total={total_time:.2f}s**"9 unmodified linesreturn process_audio(None, text_input)# Gradio interfacewith gr.Blocks(title="Enhanced Political Q&A", theme=gr.themes.Soft()) as demo:gr.Markdown("""# 🎯 Enhanced Political Q&A System**Comprehensive answers with context** - Powered by Qwen2.5-1.5B**Features:** Whisper-tiny + Qwen2.5-1.5B + DuckDuckGo + Rich contextual responses""")with gr.Tab("🎙️ Audio Input"):7 unmodified linesaudio_submit = gr.Button("🚀 Submit Audio", variant="primary", size="lg")with gr.Column():audio_output = gr.Textbox(label="Comprehensive Answer", lines=12, show_copy_button=True)audio_time = gr.Number(label="Response Time (seconds)", precision=2)audio_submit.click(8 unmodified lineswith gr.Column():text_input = gr.Textbox(label="Type your question",placeholder="Who is the current US president?",lines=3)text_submit = gr.Button("🚀 Submit Text", variant="primary", size="lg")with gr.Column():text_output = gr.Textbox(label="Comprehensive Answer", lines=12, show_copy_button=True)text_time = gr.Number(label="Response Time (seconds)", precision=2)text_submit.click(5 unmodified linesgr.Examples(examples=[["Who won the 2024 US presidential election?"],["What is the current inflation rate in India?"],["Who is the prime minister of UK and what are their key policies?"],["Explain the latest developments in AI regulation"]],inputs=text_input)1 unmodified line# API endpoints for Pluelywith gr.Tab("🔌 Pluely Integration"):gr.Markdown("""## API Endpoints for Pluely### STT Endpoint (Audio Transcription)```curl -X POST https://archcoder-basic-app.hf.space/call/transcribe_stt \\-H "Content-Type: application/json" \\-d '{"data": ["BASE64_AUDIO_DATA"]}'```**Response:** `{"data": [{"text": "transcribed text"}]}`### AI Endpoint (Enhanced Responses)```curl -X POST https://archcoder-basic-app.hf.space/call/answer_ai \\-H "Content-Type: application/json" \\-d '{"data": ["Your question here"]}'```**Response:** `{"data": ["Comprehensive answer with context"]}`## Pluely Configuration### Custom STT Provider:```curl https://archcoder-basic-app.hf.space/call/transcribe_stt -H "Content-Type: application/json" -d '{"data": ["{{AUDIO_BASE64}}"]}'```**Response Path:** `data[0].text` | **Streaming:** OFF### Custom AI Provider:```curl https://archcoder-basic-app.hf.space/call/answer_ai -H "Content-Type: application/json" -d '{"data": ["{{TEXT}}"]}'```**Response Path:** `data[0]` | **Streaming:** OFF""")# Hidden components for API endpoints21 unmodified linesgr.Markdown("""---**Model:** Qwen2.5-1.5B-Instruct (3x larger for better answers)**Output:** 150-200 words with context and background**Date-aware:** Responses reference current date ({})🟢 = Under 5s | 🟡 = 5-7s | 🔴 = Over 7s""".format(datetime.now().strftime("%B %d, %Y")))if __name__ == "__main__":demo.queue(max_size=5)6 unmodified lines7891011121314151617181920212223242526272829303132333435363738394041424344458 unmodified lines545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741759 unmodified lines1851861871881891901911921931949 unmodified lines2042052062072082092102112122132142157 unmodified lines2232242252262272282298 unmodified lines2382392402412422432442452462472482492505 unmodified lines2562572582592602612622632642651 unmodified line26726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030121 unmodified lines3233243253263273283293303313323333346 unmodified linesimport base64import tempfileimport osimport loggingfrom datetime import datetime# Setup comprehensive logginglogging.basicConfig(level=logging.INFO,format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')logger = logging.getLogger(__name__)# Initialize modelslogger.info("Loading Whisper model...")whisper_model = WhisperModel("tiny", device="cpu", compute_type="int8")logger.info("Loading Phi-2 model (faster inference)...")model_name = "microsoft/phi-2" # 2.7B - Faster CPU inference than Qwentokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)model = AutoModelForCausalLM.from_pretrained(model_name,torch_dtype=torch.float32,device_map="cpu",low_cpu_mem_usage=True,trust_remote_code=True)tokenizer.pad_token = tokenizer.eos_token# Initialize DuckDuckGo Searchddgs = DDGS(timeout=3)logger.info("All models loaded successfully!")def search_web(query, max_results=3):"""Perform web search using DuckDuckGo"""logger.info(f"[SEARCH] Query: {query}")try:results = ddgs.text(keywords=query,8 unmodified linestitle = result.get('title', '')body = result.get('body', '')context += f"\n[Source {i}] {title}\n{body}\n"logger.info(f"[SEARCH] Result {i}: {title[:50]}...")if not context:logger.warning("[SEARCH] No results found!")return "No search results found."logger.info(f"[SEARCH] Successfully retrieved {max_results} results")return context.strip()except Exception as e:logger.error(f"[SEARCH] Error: {str(e)}")return f"Search failed: {str(e)}"def transcribe_audio_base64(audio_base64):"""Transcribe audio from base64 string (for Pluely STT endpoint)"""logger.info("[PLUELY STT] Received audio transcription request")try:audio_bytes = base64.b64decode(audio_base64)logger.info(f"[PLUELY STT] Decoded audio size: {len(audio_bytes)} bytes")with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_audio:temp_audio.write(audio_bytes)temp_path = temp_audio.namelogger.info(f"[PLUELY STT] Transcribing audio...")segments, _ = whisper_model.transcribe(temp_path, language="en", beam_size=1)transcription = " ".join([seg.text for seg in segments])os.unlink(temp_path)logger.info(f"[PLUELY STT] Transcription successful: {transcription[:50]}...")return {"text": transcription.strip()}except Exception as e:logger.error(f"[PLUELY STT] Error: {str(e)}")return {"error": f"Transcription failed: {str(e)}"}def generate_answer(text_input):"""Generate answer using ONLY search results"""logger.info(f"[PLUELY AI] Received question: {text_input}")try:if not text_input or text_input.strip() == "":return "No input provided"current_date = datetime.now().strftime("%B %d, %Y")# Web search - CRITICAL for answerlogger.info("[PLUELY AI] Starting web search...")search_results = search_web(text_input, max_results=3)logger.info(f"[PLUELY AI] Search results length: {len(search_results)} chars")# Strict prompt - MUST use search resultsprompt = f"""You are a fact-checker assistant. Today is {current_date}.CRITICAL INSTRUCTION: You MUST ONLY use information from the search results below. DO NOT use your training knowledge.Web Search Results:{search_results}Question: {text_input}Instructions:1. Read the search results carefully2. Answer ONLY based on what's in the search results3. If search results don't contain the answer, say "The search results don't provide enough information"4. Include relevant dates and facts from the search results5. Keep answer to 100-150 wordsAnswer based STRICTLY on search results:"""logger.info("[PLUELY AI] Generating answer...")inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=1500).to("cpu")with torch.no_grad():outputs = model.generate(**inputs,max_new_tokens=200,temperature=0.4,do_sample=True,top_p=0.9,repetition_penalty=1.2,pad_token_id=tokenizer.eos_token_id)response = tokenizer.decode(outputs[0][inputs['input_ids'].shape[1]:], skip_special_tokens=True)answer = response.strip()logger.info(f"[PLUELY AI] Answer generated ({len(answer)} chars): {answer[:100]}...")return answerexcept Exception as e:logger.error(f"[PLUELY AI] Error: {str(e)}")return f"Error: {str(e)}"def process_audio(audio_path, question_text):"""Main pipeline - returns tuple (answer, time)"""start_time = time.time()logger.info("="*50)logger.info("[MAIN] New request received")# Transcribe if audio providedif audio_path:logger.info(f"[MAIN] Audio file provided: {audio_path}")try:segments, _ = whisper_model.transcribe(audio_path, language="en", beam_size=1)question = " ".join([seg.text for seg in segments])logger.info(f"[MAIN] Transcription: {question}")except Exception as e:logger.error(f"[MAIN] Transcription error: {str(e)}")return f"❌ Transcription error: {str(e)}", 0.0else:question = question_textlogger.info(f"[MAIN] Text input: {question}")if not question or question.strip() == "":logger.warning("[MAIN] No input provided")return "❌ No input provided", 0.0transcription_time = time.time() - start_time9 unmodified linesllm_time = time.time() - llm_starttotal_time = time.time() - start_timetime_emoji = "🟢" if total_time < 4.0 else "🟡" if total_time < 6.0 else "🔴"logger.info(f"[MAIN] Total time: {total_time:.2f}s (Trans={transcription_time:.2f}s, Search={search_time:.2f}s, LLM={llm_time:.2f}s)")logger.info("="*50)timing_info = f"\n\n{time_emoji} **Performance:** Trans={transcription_time:.2f}s | Search={search_time:.2f}s | LLM={llm_time:.2f}s | **Total={total_time:.2f}s**"9 unmodified linesreturn process_audio(None, text_input)# Gradio interfacewith gr.Blocks(title="Fast Political Q&A - Phi-2", theme=gr.themes.Soft()) as demo:gr.Markdown("""# ⚡ Fast Political Q&A System**Search-grounded answers** - Powered by Phi-2 (2.7B)**Features:** Whisper-tiny + Phi-2 (fast CPU inference) + DuckDuckGo + Search-only responses""")with gr.Tab("🎙️ Audio Input"):7 unmodified linesaudio_submit = gr.Button("🚀 Submit Audio", variant="primary", size="lg")with gr.Column():audio_output = gr.Textbox(label="Search-Grounded Answer", lines=10, show_copy_button=True)audio_time = gr.Number(label="Response Time (seconds)", precision=2)audio_submit.click(8 unmodified lineswith gr.Column():text_input = gr.Textbox(label="Type your question",placeholder="Is internet shut down in Bareilly today?",lines=3)text_submit = gr.Button("🚀 Submit Text", variant="primary", size="lg")with gr.Column():text_output = gr.Textbox(label="Search-Grounded Answer", lines=10, show_copy_button=True)text_time = gr.Number(label="Response Time (seconds)", precision=2)text_submit.click(5 unmodified linesgr.Examples(examples=[["Is internet shut down in Bareilly today?"],["Who won the 2024 US presidential election?"],["What is the current inflation rate in India?"],["What happened in Israel Palestine conflict today?"]],inputs=text_input)1 unmodified line# API endpoints for Pluelywith gr.Tab("🔌 Pluely Integration"):gr.Markdown("""## API Endpoints (All requests logged in console)### STT Endpoint```curl -X POST https://archcoder-basic-app.hf.space/call/transcribe_stt \\-H "Content-Type: application/json" \\-d '{"data": ["BASE64_AUDIO_DATA"]}'```### AI Endpoint```curl -X POST https://archcoder-basic-app.hf.space/call/answer_ai \\-H "Content-Type: application/json" \\-d '{"data": ["Your question here"]}'```## Pluely Configuration**STT Provider:**```curl https://archcoder-basic-app.hf.space/call/transcribe_stt -H "Content-Type: application/json" -d '{"data": ["{{AUDIO_BASE64}}"]}'```**Response Path:** `data[0].text`**AI Provider:**```curl https://archcoder-basic-app.hf.space/call/answer_ai -H "Content-Type: application/json" -d '{"data": ["{{TEXT}}"]}'```**Response Path:** `data[0]`""")# Hidden components for API endpoints21 unmodified linesgr.Markdown("""---**Model:** Phi-2 (2.7B) - Fast CPU inference, excellent reasoning**Output:** 100-150 words based STRICTLY on web search results**Logging:** All Pluely requests logged in console (check Logs tab)🟢 = Under 4s | 🟡 = 4-6s | 🔴 = Over 6s""")if __name__ == "__main__":demo.queue(max_size=5)
app.py+89-62