Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,147 +1,20 @@
|
|
1 |
import os
|
2 |
-
import
|
3 |
-
|
4 |
-
from gradio_client import Client
|
5 |
-
import time
|
6 |
|
7 |
-
def
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
You are an AI research strategist that generates optimized search queries for investigating complex topics. When I provide a <topic>, create 10-15 search terms/phrases that would effectively discover relevant information through search engines and academic databases.
|
12 |
-
|
13 |
-
Rules for query generation:
|
14 |
-
1. Include 3 levels of specificity: broad conceptual terms, mid-range topic phrases, niche technical terms
|
15 |
-
2. Cover multiple research angles: definitions, controversies, applications, case studies, trends
|
16 |
-
3. Use both quoted exact-match phrases and natural language questions
|
17 |
-
4. Include synonyms and variant terminology
|
18 |
-
5. Avoid duplicate concepts - each query must target distinct information
|
19 |
-
6. Order queries from general to specific
|
20 |
-
|
21 |
-
Example response format:
|
22 |
-
<search_queries>
|
23 |
-
<query>[1] "generative AI" AND intellectual property</query>
|
24 |
-
<query>[2] Training data sourcing legality LLM</query>
|
25 |
-
...
|
26 |
-
</search_queries>
|
27 |
-
|
28 |
-
Now process this topic:
|
29 |
-
<topic>{topic}</topic>
|
30 |
-
</Instructions>
|
31 |
-
"""
|
32 |
-
try:
|
33 |
-
response = DDGS().chat(prompt, model='o3-mini')
|
34 |
-
queries = []
|
35 |
-
|
36 |
-
for part in response.split("</query>"):
|
37 |
-
if "<query>" in part:
|
38 |
-
query_text = part.split("<query>")[-1].strip()
|
39 |
-
if query_text:
|
40 |
-
clean_query = query_text.split("] ", 1)[-1] if "] " in query_text else query_text
|
41 |
-
queries.append(clean_query)
|
42 |
-
|
43 |
-
if not queries:
|
44 |
-
return [f"{topic} historical analysis",
|
45 |
-
f"{topic} primary sources",
|
46 |
-
f"{topic} geopolitical impact"]
|
47 |
-
|
48 |
-
return queries[:15]
|
49 |
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
def conduct_research(query):
|
55 |
-
"""Conduct deep research on a single query"""
|
56 |
-
client = Client("m-ric/open_Deep-Research")
|
57 |
-
client.predict(query, api_name="/log_user_message")
|
58 |
-
research_data = client.predict([], api_name="/interact_with_agent")
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
def synthesize_results(original_query, queries, findings):
|
66 |
-
"""Synthesize research findings into final summary"""
|
67 |
-
synthesis_prompt = f"""
|
68 |
-
<Inputs>
|
69 |
-
Original Query: {original_query}
|
70 |
-
Research Queries: {queries}
|
71 |
-
Research Findings: {findings}
|
72 |
-
</Inputs>
|
73 |
-
|
74 |
-
<Instructions>
|
75 |
-
You are an analytical research synthesizer. Merge these findings into one cohesive summary:
|
76 |
-
|
77 |
-
1. Start with 1 paragraph overview
|
78 |
-
2. Bullet points of key findings (minimum 5)
|
79 |
-
3. 1 paragraph synthesis connecting findings to original query
|
80 |
-
4. "Additional Notes" section for peripheral but useful details
|
81 |
-
|
82 |
-
Rules:
|
83 |
-
- Include EVERY relevant data point
|
84 |
-
- Natural conversational English but professional
|
85 |
-
- No markdown formatting
|
86 |
-
- Keep paragraphs under 5 sentences
|
87 |
-
|
88 |
-
Example structure:
|
89 |
-
"Three separate analyses concur... [specific data]... This suggests... [connection to query]..."
|
90 |
-
|
91 |
-
Begin by confirming understanding of the core query, then proceed with synthesis.
|
92 |
-
</Instructions>
|
93 |
-
"""
|
94 |
-
|
95 |
-
synthesizer = Client("MiniMaxAI/MiniMax-Text-01")
|
96 |
-
return synthesizer.predict(
|
97 |
-
message=synthesis_prompt,
|
98 |
-
max_tokens=1000,
|
99 |
-
temperature=0.1,
|
100 |
-
top_p=0.9,
|
101 |
-
api_name="/chat"
|
102 |
-
)
|
103 |
-
|
104 |
-
def deep_research_agent(topic):
|
105 |
-
queries = generate_search_queries(topic)
|
106 |
-
print(f"🔍 Generated {len(queries)} research queries")
|
107 |
-
|
108 |
-
findings = []
|
109 |
-
for i, query in enumerate(queries, 1):
|
110 |
-
print(f"⏳ Researching query {i}/{len(queries)}: {query}")
|
111 |
-
findings.append(conduct_research(query))
|
112 |
-
time.sleep(1)
|
113 |
-
|
114 |
-
print("🧠 Synthesizing findings...")
|
115 |
-
return synthesize_results(topic, queries, findings)
|
116 |
-
|
117 |
-
def create_interface():
|
118 |
-
with gr.Blocks(analytics_enabled=False) as app:
|
119 |
-
gr.Markdown("# Stealth Research Assistant")
|
120 |
-
with gr.Row():
|
121 |
-
topic_input = gr.Textbox(label="Research Topic", max_lines=1)
|
122 |
-
submit_btn = gr.Button("Start Analysis", variant="primary")
|
123 |
-
|
124 |
-
status = gr.Textbox(label="Operation Status", value="Ready", interactive=False)
|
125 |
-
output = gr.Textbox(label="Final Report", lines=15, interactive=False)
|
126 |
-
|
127 |
-
@submit_btn.click(inputs=topic_input, outputs=[output, status], api_name=False)
|
128 |
-
def execute_analysis(topic):
|
129 |
-
try:
|
130 |
-
yield ["", "Analyzing topic..."]
|
131 |
-
result = deep_research_agent(topic)
|
132 |
-
yield [result, "Completed"]
|
133 |
-
except Exception as e:
|
134 |
-
yield ["", f"Error: {str(e)[:200]}"]
|
135 |
-
|
136 |
-
return app
|
137 |
-
|
138 |
-
def launch():
|
139 |
-
interface = create_interface()
|
140 |
-
interface.queue().launch(
|
141 |
-
server_name=os.getenv("SERVER_HOST", "127.0.0.1"),
|
142 |
-
server_port=int(os.getenv("SERVER_PORT", "7860")),
|
143 |
-
show_api=False
|
144 |
-
)
|
145 |
|
146 |
if __name__ == "__main__":
|
147 |
-
|
|
|
1 |
import os
|
2 |
+
import tempfile
|
3 |
+
import importlib.util
|
|
|
|
|
4 |
|
5 |
+
def load_app():
|
6 |
+
code = os.getenv("APP_CODE")
|
7 |
+
if not code:
|
8 |
+
raise RuntimeError("No application code found")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
with tempfile.NamedTemporaryFile(suffix=".py", delete=False) as f:
|
11 |
+
f.write(code.encode('utf-8'))
|
12 |
+
tmp_name = f.name
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
+
spec = importlib.util.spec_from_file_location("hidden_app", tmp_name)
|
15 |
+
module = importlib.util.module_from_spec(spec)
|
16 |
+
spec.loader.exec_module(module)
|
17 |
+
module.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
if __name__ == "__main__":
|
20 |
+
load_app()
|