import gradio as gr from ddgs import DDGS from gradio_client import Client ai_client = Client("Sachin5112/Fast-AI") def web_search_logic(query): yield "🔍 Deep searching news...", "" combined_results = "" try: with DDGS() as ddgs: # 'timelimit="d"' ka matlab hai sirf pichle 24 ghante ki news # 'region="wt-wt"' for global news ddg_results = list(ddgs.text(f"{query} latest news", max_results=5, timelimit="d")) for i, r in enumerate(ddg_results): # Sirf tabhi add karein agar content actual news jaisa dikhe if len(r.get('body', '')) > 50: combined_results += f"RESULT {i+1}:\nTitle: {r.get('title')}\nDetails: {r.get('body')}\n\n" except Exception: combined_results = "Searching failed." if not combined_results or len(combined_results) < 100: yield "⚠️ Poor search results. Trying anyway...", combined_results else: yield "✅ Quality News Found. AI Analyzing...", combined_results # --- AI PROMPT FIX --- try: # Strict instructions for the AI ai_prompt = f"""[SYSTEM: YOU ARE A NEWS BOT. DO NOT REPEAT THE INPUT.] TASK: Summarize the following REAL-TIME search data about '{query}'. DATA: {combined_results} OUTPUT FORMAT: - Use bold headings for main news. - 3-4 detailed bullet points. - Mention specific dates/names found in the text. ANSWER:""" # API Call ai_response = ai_client.predict(message=ai_prompt, api_name="/generate_response") if isinstance(ai_response, list): ai_response = ai_response[-1] # Agar AI response ab bhi chota hai, toh warn karein if len(str(ai_response)) < 50: ai_response = "⚠️ AI gave a very short response. Check if 'Max Tokens' in your Fast-AI Space is too low." final_output = f"### 🤖 AI Summary\n{ai_response}\n\n---\n### 🌐 Raw Search Data\n{combined_results}" yield "✅ Finished!", final_output except Exception as e: yield "❌ AI Error", f"AI Space is not responding correctly: {str(e)}\n\nCheck Raw Data:\n{combined_results}" with gr.Blocks(title="SMART SEARCH") as demo: gr.Markdown("# 🚀 Pro AI Web Search") with gr.Row(): input_text = gr.Textbox(label="Query", placeholder="OpenAI latest news") status_box = gr.Textbox(label="Live Status", interactive=False) submit_btn = gr.Button("Search & Summarize", variant="primary") output_markdown = gr.Markdown() submit_btn.click(fn=web_search_logic, inputs=input_text, outputs=[status_box, output_markdown]) demo.queue().launch()