Svngoku commited on
Commit
97379bb
·
verified ·
1 Parent(s): 7d6c86f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -56
app.py CHANGED
@@ -1,7 +1,17 @@
1
  # unsloth_doc_app.py
2
  import streamlit as st
3
  import os
4
- from smolagents import CodeAgent, Tool
 
 
 
 
 
 
 
 
 
 
5
  import requests
6
  import json
7
  from typing import List, Dict, Any
@@ -19,43 +29,6 @@ AUTHORIZED_IMPORTS: List[str] = [
19
  'pandas', 'numpy', 'requests'
20
  ]
21
 
22
- # Tools
23
- class DuckDuckGoSearchTool(Tool):
24
- name = "duckduckgo_search"
25
- description = "Searches DuckDuckGo for Unsloth-related queries."
26
- inputs = {"query": {"type": "string", "description": "The search query"}}
27
- output_type = "string"
28
-
29
- def __init__(self):
30
- super().__init__()
31
- self.url = "https://api.duckduckgo.com/"
32
- self.headers = {'Content-Type': 'application/json'}
33
-
34
- def forward(self, query: str) -> str:
35
- payload = {"q": f"unsloth {query}", "format": "json", "no_html": 1}
36
- response = requests.get(self.url, params=payload, headers=self.headers)
37
- if response.status_code == 200:
38
- return json.dumps(response.json(), indent=2)
39
- return "Error: Unable to fetch search results."
40
-
41
- class VisitWebpageTool(Tool):
42
- name = "visit_webpage"
43
- description = "Fetches content from a specific webpage."
44
- inputs = {"url": {"type": "string", "description": "The URL to visit"}}
45
- output_type = "string"
46
-
47
- def __init__(self):
48
- super().__init__()
49
- self.headers = {'User-Agent': 'Mozilla/5.0'}
50
-
51
- def forward(self, url: str) -> str:
52
- try:
53
- response = requests.get(url, headers=self.headers)
54
- response.raise_for_status()
55
- return response.text[:2000] # Limit to first 2000 characters
56
- except Exception as e:
57
- return f"Error visiting webpage: {e}"
58
-
59
  # Agent Initialization
60
  def initialize_unsloth_agent(model=None):
61
  """Initialize the Unsloth Documentation Agent"""
@@ -65,8 +38,7 @@ def initialize_unsloth_agent(model=None):
65
  ]
66
  if model is None:
67
  from smolagents import HfApiModel
68
- model = HfApiModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct",provider="together",)
69
-
70
 
71
  return CodeAgent(
72
  name="unsloth_agent",
@@ -128,22 +100,24 @@ def main():
128
  placeholder="E.g., How to fine-tune with Unsloth"
129
  )
130
 
131
- if st.button("Search", type="primary", key="search_button_with_query") and search_query:
132
- with st.spinner("Searching Unsloth documentation..."):
133
- try:
134
- results = st.session_state.agent.run(search_query)
135
- display_results(results)
136
-
137
- st.markdown("---")
138
- st.info("""
139
- 💡 **How to read the results:**
140
- - Results may include JSON from searches or webpage snippets.
141
- - Check the sidebar for tips on refining your query.
142
- """)
143
- except Exception as e:
144
- st.error(f"An error occurred: {e}")
145
- elif st.button("Search", type="primary", key="search_button_no_query"):
146
- st.warning("Please enter a search query.")
 
 
147
 
148
  st.markdown("---")
149
  st.caption("Powered by SmolAgents and Unsloth")
 
1
  # unsloth_doc_app.py
2
  import streamlit as st
3
  import os
4
+ from smolagents import (
5
+ CodeAgent,
6
+ ToolCallingAgent,
7
+ HfApiModel,
8
+ DuckDuckGoSearchTool,
9
+ LiteLLMModel,
10
+ tool,
11
+ VisitWebpageTool,
12
+ GradioUI,
13
+ PythonInterpreterTool
14
+ )
15
  import requests
16
  import json
17
  from typing import List, Dict, Any
 
29
  'pandas', 'numpy', 'requests'
30
  ]
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  # Agent Initialization
33
  def initialize_unsloth_agent(model=None):
34
  """Initialize the Unsloth Documentation Agent"""
 
38
  ]
39
  if model is None:
40
  from smolagents import HfApiModel
41
+ model = HfApiModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct", provider="together")
 
42
 
43
  return CodeAgent(
44
  name="unsloth_agent",
 
100
  placeholder="E.g., How to fine-tune with Unsloth"
101
  )
102
 
103
+ # Single button to handle both cases
104
+ if st.button("Search", type="primary", key="search_button"):
105
+ if search_query:
106
+ with st.spinner("Searching Unsloth documentation..."):
107
+ try:
108
+ results = st.session_state.agent.run(search_query, additional_args={"sources" : ["https://docs.unsloth.ai/"],"instructions": "Response to the user answer as a structured documentation format."})
109
+ display_results(results)
110
+
111
+ st.markdown("---")
112
+ st.info("""
113
+ 💡 **How to read the results:**
114
+ - Results may include JSON from searches or webpage snippets.
115
+ - Check the sidebar for tips on refining your query.
116
+ """)
117
+ except Exception as e:
118
+ st.error(f"An error occurred: {e}")
119
+ else:
120
+ st.warning("Please enter a search query.")
121
 
122
  st.markdown("---")
123
  st.caption("Powered by SmolAgents and Unsloth")