Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from gradio_client import Client
|
| 3 |
+
from bs4 import BeautifulSoup
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Initialize the client for the OpenResearcher space
|
| 7 |
+
client = Client("OpenResearcher/OpenResearcher")
|
| 8 |
+
|
| 9 |
+
def research_topic(question):
|
| 10 |
+
# 1. Get the API Key from Hugging Face Secrets (Environment Variables)
|
| 11 |
+
# If testing locally, you can replace os.getenv with your actual string,
|
| 12 |
+
# but for HF Spaces, use the Secret to keep it safe.
|
| 13 |
+
api_key = os.getenv("SERPER_KEY")
|
| 14 |
+
|
| 15 |
+
if not api_key:
|
| 16 |
+
return "Error: SERPER_KEY not found in Environment Variables. Please add it in Space Settings."
|
| 17 |
+
|
| 18 |
+
print(f"Researching: {question}...")
|
| 19 |
+
|
| 20 |
+
try:
|
| 21 |
+
# 2. Call the OpenResearcher API
|
| 22 |
+
result = client.predict(
|
| 23 |
+
question=question,
|
| 24 |
+
serper_key=api_key,
|
| 25 |
+
max_rounds=50,
|
| 26 |
+
api_name="/start_research"
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
# The API returns [user_input, full_html_log] at indices 0 and 1
|
| 30 |
+
raw_html = result[1]
|
| 31 |
+
|
| 32 |
+
# 3. Parse the HTML to find only the "answer-section"
|
| 33 |
+
soup = BeautifulSoup(raw_html, 'html.parser')
|
| 34 |
+
|
| 35 |
+
# Look for the div containing the final conclusion
|
| 36 |
+
answer_section = soup.find('div', class_='answer-section')
|
| 37 |
+
|
| 38 |
+
if answer_section:
|
| 39 |
+
# Return the clean HTML
|
| 40 |
+
return str(answer_section)
|
| 41 |
+
else:
|
| 42 |
+
# Fallback: Sometimes the API fails or returns a different format
|
| 43 |
+
return f"<div>Could not extract a final answer. Raw output length: {len(raw_html)} characters.</div>"
|
| 44 |
+
|
| 45 |
+
except Exception as e:
|
| 46 |
+
return f"An error occurred: {str(e)}"
|
| 47 |
+
|
| 48 |
+
# 4. Create the Gradio Interface
|
| 49 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 50 |
+
gr.Markdown("# 🧠 Deep Search Conclusion Generator")
|
| 51 |
+
gr.Markdown("Enter a question. This app searches the web using OpenResearcher and filters out the thinking process to give you **only the final result**.")
|
| 52 |
+
|
| 53 |
+
with gr.Row():
|
| 54 |
+
inp = gr.Textbox(label="Your Question", placeholder="e.g. Top 10 Closest Black Holes to Earth")
|
| 55 |
+
btn = gr.Button("Start Research", variant="primary")
|
| 56 |
+
|
| 57 |
+
# HTML component renders the links and bold text correctly
|
| 58 |
+
out = gr.HTML(label="Final Conclusion")
|
| 59 |
+
|
| 60 |
+
btn.click(fn=research_topic, inputs=inp, outputs=out)
|
| 61 |
+
|
| 62 |
+
if __name__ == "__main__":
|
| 63 |
+
demo.launch()
|