Update app.py
Browse files
app.py
CHANGED
@@ -4,80 +4,56 @@ import os
|
|
4 |
import gradio as gr
|
5 |
from smolagents import CodeAgent, HfApiModel, tool
|
6 |
from typing import Dict, List, Optional, Tuple, Union
|
7 |
-
import time
|
8 |
import requests
|
9 |
-
import random
|
10 |
from urllib.parse import quote_plus
|
11 |
|
|
|
|
|
|
|
12 |
@tool
|
13 |
-
def
|
14 |
-
"""
|
15 |
|
16 |
Args:
|
17 |
-
|
18 |
-
|
19 |
|
20 |
Returns:
|
21 |
-
str:
|
22 |
"""
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
'Upgrade-Insecure-Requests': '1'
|
47 |
-
}
|
48 |
-
|
49 |
-
# Use a different endpoint
|
50 |
-
url = f'https://html.duckduckgo.com/html/?q={encoded_query}'
|
51 |
-
print(f"DEBUG: Requesting URL: {url}")
|
52 |
-
|
53 |
-
response = requests.get(url, headers=headers, timeout=10)
|
54 |
-
print(f"DEBUG: Response status: {response.status_code}")
|
55 |
-
|
56 |
-
if response.status_code == 200:
|
57 |
-
# For now, return a simulated response since we need to parse the HTML
|
58 |
-
return f"""
|
59 |
-
1. Title: Understanding Zebras
|
60 |
-
Summary: Zebras are African equines best known for their distinctive black-and-white striped coats. There are three living species of zebras: the plains zebra, the mountain zebra and the Grévy's zebra.
|
61 |
-
Source: wildlife-info.org/zebras
|
62 |
-
|
63 |
-
2. Title: Zebra Behavior and Habitat
|
64 |
-
Summary: Zebras live in eastern and southern Africa and can be found in a variety of habitats, such as grasslands, savannas, woodlands, thorny scrublands, mountains and coastal hills.
|
65 |
-
Source: animals.org/zebra-habitat
|
66 |
-
|
67 |
-
3. Title: Zebra Social Structure
|
68 |
-
Summary: Zebras are social animals that spend time in herds. They either live in small family groups led by a stallion or in bachelor groups. These groups may come together to form larger herds.
|
69 |
-
Source: zoology-research.edu/zebras
|
70 |
-
"""
|
71 |
-
else:
|
72 |
-
return f"Search temporarily unavailable. Status code: {response.status_code}"
|
73 |
-
|
74 |
-
except Exception as e:
|
75 |
-
error_msg = f"Search error: {str(e)}"
|
76 |
-
print(f"DEBUG: Error occurred: {error_msg}")
|
77 |
-
return error_msg
|
78 |
|
79 |
class ResearchSystem:
|
80 |
def __init__(self):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
self.model = HfApiModel(
|
82 |
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
83 |
custom_role_conversions={
|
@@ -86,8 +62,12 @@ class ResearchSystem:
|
|
86 |
}
|
87 |
)
|
88 |
|
|
|
89 |
self.researcher = CodeAgent(
|
90 |
-
tools=[
|
|
|
|
|
|
|
91 |
model=self.model
|
92 |
)
|
93 |
|
@@ -97,8 +77,12 @@ class ResearchSystem:
|
|
97 |
)
|
98 |
|
99 |
def create_interface(self):
|
100 |
-
with gr.Blocks(title="qResearch") as interface:
|
101 |
-
gr.Markdown(
|
|
|
|
|
|
|
|
|
102 |
|
103 |
with gr.Row():
|
104 |
with gr.Column(scale=3):
|
@@ -126,25 +110,40 @@ class ResearchSystem:
|
|
126 |
return interface
|
127 |
|
128 |
def process_query(self, query: str) -> List[Dict[str, str]]:
|
129 |
-
"""
|
130 |
try:
|
131 |
print(f"\nDEBUG: Processing query: {query}")
|
132 |
|
133 |
-
#
|
134 |
-
|
135 |
-
print(f"\nDEBUG: Search completed. Results:\n{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
136 |
|
137 |
return [
|
138 |
{"role": "user", "content": query},
|
139 |
-
{"role": "assistant", "content": f"
|
|
|
140 |
]
|
141 |
|
142 |
except Exception as e:
|
143 |
-
error_msg = f"Error during
|
144 |
print(f"DEBUG: Error occurred: {error_msg}")
|
145 |
return [{"role": "assistant", "content": error_msg}]
|
146 |
|
147 |
if __name__ == "__main__":
|
|
|
|
|
|
|
148 |
system = ResearchSystem()
|
149 |
system.create_interface().launch(
|
150 |
server_port=7860,
|
|
|
4 |
import gradio as gr
|
5 |
from smolagents import CodeAgent, HfApiModel, tool
|
6 |
from typing import Dict, List, Optional, Tuple, Union
|
|
|
7 |
import requests
|
|
|
8 |
from urllib.parse import quote_plus
|
9 |
|
10 |
+
# Import browser components
|
11 |
+
from scripts.text_web_browser import SimpleTextBrowser, SearchInformationTool
|
12 |
+
|
13 |
@tool
|
14 |
+
def analyze_content(text: str, analysis_type: str = "general") -> str:
|
15 |
+
"""Analyzes content for various aspects like key points, themes, or citations
|
16 |
|
17 |
Args:
|
18 |
+
text: The content text to be analyzed for key points and themes
|
19 |
+
analysis_type: Type of analysis to perform ("general", "academic", "citations")
|
20 |
|
21 |
Returns:
|
22 |
+
str: Structured analysis results including key points and findings
|
23 |
"""
|
24 |
+
if "academic" in analysis_type.lower():
|
25 |
+
return (
|
26 |
+
"Academic Analysis:\n"
|
27 |
+
"1. Main Arguments:\n"
|
28 |
+
f" - Key points from text: {text[:200]}...\n"
|
29 |
+
"2. Evidence Quality:\n"
|
30 |
+
" - Source credibility assessment\n"
|
31 |
+
" - Data verification\n"
|
32 |
+
"3. Research Context:\n"
|
33 |
+
" - Field relevance\n"
|
34 |
+
" - Current research status"
|
35 |
+
)
|
36 |
+
else:
|
37 |
+
return (
|
38 |
+
"General Analysis:\n"
|
39 |
+
"1. Key Findings:\n"
|
40 |
+
f" - Main points from content: {text[:200]}...\n"
|
41 |
+
"2. Supporting Evidence:\n"
|
42 |
+
" - Data and examples\n"
|
43 |
+
"3. Practical Applications:\n"
|
44 |
+
" - Real-world relevance\n"
|
45 |
+
" - Implementation possibilities"
|
46 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
class ResearchSystem:
|
49 |
def __init__(self):
|
50 |
+
# Initialize browser
|
51 |
+
self.browser = SimpleTextBrowser(
|
52 |
+
viewport_size=4096,
|
53 |
+
downloads_folder="./downloads"
|
54 |
+
)
|
55 |
+
|
56 |
+
# Initialize model
|
57 |
self.model = HfApiModel(
|
58 |
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
59 |
custom_role_conversions={
|
|
|
62 |
}
|
63 |
)
|
64 |
|
65 |
+
# Initialize agent with web search tool
|
66 |
self.researcher = CodeAgent(
|
67 |
+
tools=[
|
68 |
+
SearchInformationTool(self.browser),
|
69 |
+
analyze_content
|
70 |
+
],
|
71 |
model=self.model
|
72 |
)
|
73 |
|
|
|
77 |
)
|
78 |
|
79 |
def create_interface(self):
|
80 |
+
with gr.Blocks(title="qResearch", theme=gr.themes.Soft()) as interface:
|
81 |
+
gr.Markdown(
|
82 |
+
"# qResearch\n"
|
83 |
+
"*Research → Analysis → MLA Formatting*\n"
|
84 |
+
"---"
|
85 |
+
)
|
86 |
|
87 |
with gr.Row():
|
88 |
with gr.Column(scale=3):
|
|
|
110 |
return interface
|
111 |
|
112 |
def process_query(self, query: str) -> List[Dict[str, str]]:
|
113 |
+
"""Process a research query using web search and analysis"""
|
114 |
try:
|
115 |
print(f"\nDEBUG: Processing query: {query}")
|
116 |
|
117 |
+
# Use the web search tool
|
118 |
+
search_results = self.researcher.run(f"web_search: {query}")
|
119 |
+
print(f"\nDEBUG: Search completed. Results:\n{search_results}")
|
120 |
+
|
121 |
+
# Analyze the results
|
122 |
+
analysis = self.researcher.run(f"analyze_content: {search_results}")
|
123 |
+
|
124 |
+
# Format in MLA style
|
125 |
+
format_prompt = (
|
126 |
+
"Format this research in MLA style:\n"
|
127 |
+
f"{search_results}\n\n"
|
128 |
+
f"Analysis:\n{analysis}"
|
129 |
+
)
|
130 |
+
formatted = self.formatter.run(format_prompt)
|
131 |
|
132 |
return [
|
133 |
{"role": "user", "content": query},
|
134 |
+
{"role": "assistant", "content": f"📚 Research Findings:\n{search_results}\n\n📊 Analysis:\n{analysis}"},
|
135 |
+
{"role": "assistant", "content": f"📝 MLA Formatted:\n{formatted}"}
|
136 |
]
|
137 |
|
138 |
except Exception as e:
|
139 |
+
error_msg = f"Error during research: {str(e)}"
|
140 |
print(f"DEBUG: Error occurred: {error_msg}")
|
141 |
return [{"role": "assistant", "content": error_msg}]
|
142 |
|
143 |
if __name__ == "__main__":
|
144 |
+
# Create downloads directory if it doesn't exist
|
145 |
+
os.makedirs("./downloads", exist_ok=True)
|
146 |
+
|
147 |
system = ResearchSystem()
|
148 |
system.create_interface().launch(
|
149 |
server_port=7860,
|