Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,15 @@
|
|
1 |
-
"""qResearch:
|
2 |
|
3 |
-
import argparse
|
4 |
-
import json
|
5 |
import os
|
6 |
-
import threading
|
7 |
-
from concurrent.futures import ThreadPoolExecutor, as_completed
|
8 |
-
from datetime import datetime
|
9 |
-
from pathlib import Path
|
10 |
-
from typing import List, Optional
|
11 |
-
|
12 |
-
import datasets
|
13 |
-
import pandas as pd
|
14 |
-
from dotenv import load_dotenv
|
15 |
import gradio as gr
|
|
|
|
|
16 |
|
17 |
-
|
18 |
-
CodeAgent,
|
19 |
-
HfApiModel,
|
20 |
-
ToolCallingAgent,
|
21 |
-
Tool
|
22 |
-
)
|
23 |
-
from smolagents.agent_types import AgentText, AgentImage, AgentAudio
|
24 |
-
|
25 |
-
# Configuration setup
|
26 |
load_dotenv(override=True)
|
27 |
|
28 |
class DuckDuckGoSearchTool(Tool):
|
29 |
-
"""
|
30 |
name = "web_search"
|
31 |
description = "Performs privacy-focused web searches using DuckDuckGo"
|
32 |
inputs = {
|
@@ -37,81 +20,132 @@ class DuckDuckGoSearchTool(Tool):
|
|
37 |
|
38 |
def forward(self, query: str, max_results: int = 5) -> str:
|
39 |
from duckduckgo_search import DDGS
|
40 |
-
|
41 |
with DDGS() as ddgs:
|
42 |
results = list(ddgs.text(query, max_results=max_results))
|
43 |
-
|
44 |
-
|
45 |
-
f"{idx+1}. {result['title']}\n"
|
46 |
-
f" URL: {result['href']}\n"
|
47 |
-
f" Summary: {result['body']}\n"
|
48 |
-
for idx, result in enumerate(results)
|
49 |
-
]
|
50 |
-
return "## Web Research Results\n" + "\n".join(formatted_results)
|
51 |
|
52 |
class ResearchSystem:
|
53 |
def __init__(self):
|
|
|
54 |
self.model = HfApiModel(
|
55 |
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
56 |
custom_role_conversions={"tool-call": "assistant", "tool-response": "user"},
|
57 |
use_auth_token=False,
|
58 |
trust_remote_code=True
|
59 |
)
|
60 |
-
|
|
|
|
|
61 |
tools=[DuckDuckGoSearchTool()],
|
62 |
-
model=self.model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
)
|
64 |
-
self.file_storage = "research_documents"
|
65 |
-
os.makedirs(self.file_storage, exist_ok=True)
|
66 |
|
67 |
-
def
|
68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
|
70 |
def create_interface(self):
|
71 |
-
with gr.Blocks(theme=gr.themes.
|
72 |
-
gr.Markdown("# qResearch\n*
|
73 |
|
74 |
with gr.Row():
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
with gr.Row():
|
83 |
input_box = gr.Textbox(
|
84 |
-
placeholder="Enter
|
85 |
-
lines=
|
86 |
-
max_lines=6,
|
87 |
label="Research Query"
|
88 |
)
|
89 |
-
submit_btn = gr.Button("
|
90 |
|
91 |
submit_btn.click(
|
92 |
-
self.
|
93 |
inputs=[input_box],
|
94 |
-
outputs=[chat_history,
|
95 |
)
|
96 |
|
97 |
return interface
|
98 |
|
99 |
-
def
|
100 |
try:
|
101 |
-
|
102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
return [
|
104 |
-
|
105 |
-
|
106 |
-
],
|
|
|
|
|
|
|
|
|
107 |
except Exception as e:
|
108 |
-
error_msg = f"
|
109 |
-
return [
|
110 |
-
gr.ChatMessage(role="user", content=query),
|
111 |
-
gr.ChatMessage(role="assistant", content=error_msg)
|
112 |
-
], error_msg
|
113 |
|
114 |
if __name__ == "__main__":
|
115 |
-
|
116 |
-
|
117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""qResearch v3.0: Dual-Agent Research System"""
|
2 |
|
|
|
|
|
3 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
import gradio as gr
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
from smolagents import CodeAgent, HfApiModel, Tool
|
7 |
|
8 |
+
# Initialize environment
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
load_dotenv(override=True)
|
10 |
|
11 |
class DuckDuckGoSearchTool(Tool):
|
12 |
+
"""Web search tool for primary research"""
|
13 |
name = "web_search"
|
14 |
description = "Performs privacy-focused web searches using DuckDuckGo"
|
15 |
inputs = {
|
|
|
20 |
|
21 |
def forward(self, query: str, max_results: int = 5) -> str:
|
22 |
from duckduckgo_search import DDGS
|
|
|
23 |
with DDGS() as ddgs:
|
24 |
results = list(ddgs.text(query, max_results=max_results))
|
25 |
+
return "\n".join([f"{idx+1}. {r['title']} ({r['href']}): {r['body']}"
|
26 |
+
for idx, r in enumerate(results)])
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
class ResearchSystem:
|
29 |
def __init__(self):
|
30 |
+
# Shared model configuration
|
31 |
self.model = HfApiModel(
|
32 |
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
33 |
custom_role_conversions={"tool-call": "assistant", "tool-response": "user"},
|
34 |
use_auth_token=False,
|
35 |
trust_remote_code=True
|
36 |
)
|
37 |
+
|
38 |
+
# Initialize Researcher Agent
|
39 |
+
self.researcher = CodeAgent(
|
40 |
tools=[DuckDuckGoSearchTool()],
|
41 |
+
model=self.model,
|
42 |
+
system_prompt="""You are a Senior Research Analyst. Your tasks:
|
43 |
+
1. Conduct comprehensive web research using available tools
|
44 |
+
2. Synthesize findings into a detailed draft report
|
45 |
+
3. Include all relevant sources and data points
|
46 |
+
4. Maintain raw factual accuracy without formatting"""
|
47 |
+
)
|
48 |
+
|
49 |
+
# Initialize Formatter Agent
|
50 |
+
self.formatter = CodeAgent(
|
51 |
+
tools=[],
|
52 |
+
model=self.model,
|
53 |
+
system_prompt="""You are an MLA Formatting Specialist. Your tasks:
|
54 |
+
1. Receive raw research content
|
55 |
+
2. Apply proper MLA 9th edition formatting
|
56 |
+
3. Verify citation integrity
|
57 |
+
4. Structure content with:
|
58 |
+
- Header block
|
59 |
+
- Title capitalization
|
60 |
+
- In-text citations
|
61 |
+
- Works Cited section
|
62 |
+
5. Ensure academic tone and clarity"""
|
63 |
)
|
|
|
|
|
64 |
|
65 |
+
def _process_research(self, query: str) -> str:
|
66 |
+
"""Execute two-stage research process"""
|
67 |
+
# Stage 1: Initial research
|
68 |
+
raw_response = self.researcher.run(
|
69 |
+
task=f"Conduct research about: {query}",
|
70 |
+
temperature=0.7
|
71 |
+
)
|
72 |
+
|
73 |
+
# Stage 2: MLA formatting
|
74 |
+
formatted_response = self.formatter.run(
|
75 |
+
task=f"Format this research into MLA:\n{raw_response}",
|
76 |
+
temperature=0.3
|
77 |
+
)
|
78 |
+
|
79 |
+
return formatted_response
|
80 |
|
81 |
def create_interface(self):
|
82 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="qResearch v3") as interface:
|
83 |
+
gr.Markdown("# qResearch Dual-Agent System\n*Research → Analysis → MLA Formatting*")
|
84 |
|
85 |
with gr.Row():
|
86 |
+
with gr.Column(scale=2):
|
87 |
+
chat_history = gr.Chatbot(
|
88 |
+
label="Agent Communication",
|
89 |
+
avatar_images={
|
90 |
+
"user": "👤",
|
91 |
+
"assistant": "🤖",
|
92 |
+
"Researcher": "🔍",
|
93 |
+
"Formatter": "✒️"
|
94 |
+
},
|
95 |
+
height=500
|
96 |
+
)
|
97 |
+
|
98 |
+
with gr.Column(scale=1):
|
99 |
+
research_steps = gr.JSON(
|
100 |
+
label="Processing Steps",
|
101 |
+
value={"Current Stage": "Awaiting Input"}
|
102 |
+
)
|
103 |
|
104 |
with gr.Row():
|
105 |
input_box = gr.Textbox(
|
106 |
+
placeholder="Enter research topic...",
|
107 |
+
lines=2,
|
|
|
108 |
label="Research Query"
|
109 |
)
|
110 |
+
submit_btn = gr.Button("Start Research", variant="primary")
|
111 |
|
112 |
submit_btn.click(
|
113 |
+
self._handle_query,
|
114 |
inputs=[input_box],
|
115 |
+
outputs=[chat_history, research_steps]
|
116 |
)
|
117 |
|
118 |
return interface
|
119 |
|
120 |
+
def _handle_query(self, query: str):
|
121 |
try:
|
122 |
+
# Initial researcher agent processing
|
123 |
+
researcher_msg = gr.ChatMessage(role="Researcher", content=f"Beginning research: {query}")
|
124 |
+
|
125 |
+
# Get formatted response
|
126 |
+
formatted_response = self._process_research(query)
|
127 |
+
|
128 |
+
# Formatter agent response
|
129 |
+
formatter_msg = gr.ChatMessage(role="Formatter", content=formatted_response)
|
130 |
+
|
131 |
return [
|
132 |
+
researcher_msg,
|
133 |
+
formatter_msg
|
134 |
+
], {
|
135 |
+
"Completed Stages": ["Research Collection", "MLA Formatting"],
|
136 |
+
"Current Status": "Research Complete"
|
137 |
+
}
|
138 |
+
|
139 |
except Exception as e:
|
140 |
+
error_msg = gr.ChatMessage(role="System", content=f"Error: {str(e)}")
|
141 |
+
return [error_msg], {"Error": str(e)}
|
|
|
|
|
|
|
142 |
|
143 |
if __name__ == "__main__":
|
144 |
+
research_system = ResearchSystem()
|
145 |
+
interface = research_system.create_interface()
|
146 |
+
interface.launch(
|
147 |
+
server_name="0.0.0.0",
|
148 |
+
server_port=7860,
|
149 |
+
share=False,
|
150 |
+
show_error=True
|
151 |
+
)
|