Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -19,19 +19,19 @@ st.set_page_config(
|
|
19 |
initial_sidebar_state="expanded"
|
20 |
)
|
21 |
|
22 |
-
#
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
28 |
else:
|
29 |
-
st.warning("
|
30 |
-
|
31 |
-
# Initialize the model
|
32 |
-
return HfApiModel(model_id="meta-llama/Llama-3.3-70B-Instruct")
|
33 |
-
|
34 |
-
model = initialize_api()
|
35 |
|
36 |
# Define all your tools
|
37 |
class RandomComicFetcher(Tool):
|
@@ -103,8 +103,46 @@ prime_check_tool = PrimeCheckTool()
|
|
103 |
st.title("SmolaAgents Tools")
|
104 |
st.markdown("Select a tool from the sidebar and interact with AI-powered assistants")
|
105 |
|
106 |
-
#
|
|
|
|
|
|
|
|
|
|
|
107 |
with st.sidebar:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
st.title("Tool Selection")
|
109 |
tool_choice = st.radio(
|
110 |
"Choose a tool:",
|
@@ -120,102 +158,131 @@ with st.sidebar:
|
|
120 |
- Check if numbers are prime with creative explanations
|
121 |
""")
|
122 |
|
123 |
-
#
|
124 |
-
if
|
125 |
-
st.
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
if st.button("Search"):
|
131 |
-
if query:
|
132 |
-
with st.spinner("Searching and processing..."):
|
133 |
-
agent = CodeAgent(tools=[search_tool], model=model)
|
134 |
-
response = agent.run(query)
|
135 |
-
st.success("Search complete!")
|
136 |
-
st.markdown("### Results")
|
137 |
-
st.markdown(response)
|
138 |
-
else:
|
139 |
-
st.warning("Please enter a search query")
|
140 |
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
st.
|
147 |
-
|
148 |
-
st.
|
149 |
-
|
150 |
-
st.
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
167 |
with col1:
|
168 |
-
st.
|
169 |
-
|
170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
171 |
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
|
|
176 |
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
|
|
|
|
|
|
|
|
|
|
183 |
|
184 |
-
elif tool_choice == "Prime Number Checker":
|
185 |
-
|
186 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
187 |
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
["Poetic", "Nursery Rhyme", "Scientific", "Humorous", "Historical"]
|
192 |
-
)
|
193 |
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
st.markdown(f"### Result: {prime_status}")
|
207 |
-
|
208 |
-
st.markdown("### AI Explanation")
|
209 |
-
st.markdown(response)
|
210 |
-
|
211 |
-
# Optional: Show a mathematical representation
|
212 |
-
if st.checkbox("Show mathematical details"):
|
213 |
-
if is_prime:
|
214 |
-
st.markdown(f"{number} has no factors other than 1 and itself.")
|
215 |
-
else:
|
216 |
-
# Find the factors
|
217 |
-
factors = [i for i in range(1, number + 1) if number % i == 0]
|
218 |
-
st.markdown(f"Factors of {number}: {', '.join(map(str, factors))}")
|
219 |
|
220 |
# Add footer
|
221 |
st.sidebar.divider()
|
|
|
19 |
initial_sidebar_state="expanded"
|
20 |
)
|
21 |
|
22 |
+
# Function to initialize the API with user-provided token
|
23 |
+
def initialize_api(token=None):
|
24 |
+
if token:
|
25 |
+
login(token)
|
26 |
+
try:
|
27 |
+
# Initialize the model
|
28 |
+
return HfApiModel(model_id="meta-llama/Llama-3.3-70B-Instruct")
|
29 |
+
except Exception as e:
|
30 |
+
st.error(f"Error initializing model: {str(e)}")
|
31 |
+
return None
|
32 |
else:
|
33 |
+
st.warning("Please enter your Hugging Face API token to use this app.")
|
34 |
+
return None
|
|
|
|
|
|
|
|
|
35 |
|
36 |
# Define all your tools
|
37 |
class RandomComicFetcher(Tool):
|
|
|
103 |
st.title("SmolaAgents Tools")
|
104 |
st.markdown("Select a tool from the sidebar and interact with AI-powered assistants")
|
105 |
|
106 |
+
# Initialize session state for token
|
107 |
+
if "hf_token" not in st.session_state:
|
108 |
+
st.session_state.hf_token = ""
|
109 |
+
st.session_state.model = None
|
110 |
+
|
111 |
+
# Sidebar for tool selection and token input
|
112 |
with st.sidebar:
|
113 |
+
st.title("Configuration")
|
114 |
+
|
115 |
+
# Check for token in environment first
|
116 |
+
env_token = os.environ.get("HF_TOKEN")
|
117 |
+
if env_token and not st.session_state.hf_token:
|
118 |
+
st.session_state.hf_token = env_token
|
119 |
+
st.success("HF_TOKEN found in environment variables")
|
120 |
+
|
121 |
+
# Token input
|
122 |
+
token_input = st.text_input(
|
123 |
+
"Enter your Hugging Face API Token:",
|
124 |
+
value=st.session_state.hf_token,
|
125 |
+
type="password",
|
126 |
+
help="Get your token from huggingface.co/settings/tokens"
|
127 |
+
)
|
128 |
+
|
129 |
+
if token_input != st.session_state.hf_token:
|
130 |
+
st.session_state.hf_token = token_input
|
131 |
+
# Reset model when token changes
|
132 |
+
st.session_state.model = None
|
133 |
+
|
134 |
+
# Button to initialize/test the token
|
135 |
+
if st.button("Initialize API"):
|
136 |
+
with st.spinner("Testing your token..."):
|
137 |
+
model = initialize_api(st.session_state.hf_token)
|
138 |
+
if model:
|
139 |
+
st.session_state.model = model
|
140 |
+
st.success("β
API initialized successfully!")
|
141 |
+
else:
|
142 |
+
st.error("β Failed to initialize the API with the provided token.")
|
143 |
+
|
144 |
+
st.divider()
|
145 |
+
|
146 |
st.title("Tool Selection")
|
147 |
tool_choice = st.radio(
|
148 |
"Choose a tool:",
|
|
|
158 |
- Check if numbers are prime with creative explanations
|
159 |
""")
|
160 |
|
161 |
+
# Check if the model is initialized
|
162 |
+
if not st.session_state.model and st.session_state.hf_token:
|
163 |
+
with st.spinner("Initializing model..."):
|
164 |
+
st.session_state.model = initialize_api(st.session_state.hf_token)
|
165 |
+
if st.session_state.model:
|
166 |
+
st.success("Model initialized successfully!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
167 |
|
168 |
+
# Main content area - only show if token is provided
|
169 |
+
if st.session_state.hf_token and st.session_state.model:
|
170 |
+
# Main content area
|
171 |
+
if tool_choice == "Search Tool":
|
172 |
+
st.header("AI Web Search")
|
173 |
+
st.markdown("Ask any question and the AI will search the web for answers")
|
174 |
+
|
175 |
+
query = st.text_input("Enter your search query:", placeholder="What are the latest advancements in renewable energy?")
|
176 |
+
|
177 |
+
if st.button("Search"):
|
178 |
+
if query:
|
179 |
+
with st.spinner("Searching and processing..."):
|
180 |
+
agent = CodeAgent(tools=[search_tool], model=st.session_state.model)
|
181 |
+
response = agent.run(query)
|
182 |
+
st.success("Search complete!")
|
183 |
+
st.markdown("### Results")
|
184 |
+
st.markdown(response)
|
185 |
+
else:
|
186 |
+
st.warning("Please enter a search query")
|
187 |
+
|
188 |
+
elif tool_choice == "XKCD Comic Fetcher":
|
189 |
+
st.header("XKCD Comic Explorer")
|
190 |
+
st.markdown("Fetch a random XKCD comic and get AI commentary")
|
191 |
+
|
192 |
+
if "comic_fetched" not in st.session_state:
|
193 |
+
st.session_state.comic_fetched = False
|
194 |
+
st.session_state.comic_img = None
|
195 |
+
st.session_state.comic_title = ""
|
196 |
+
st.session_state.comic_alt = ""
|
197 |
+
st.session_state.comic_url = ""
|
198 |
+
|
199 |
+
col1, col2 = st.columns([1, 2])
|
200 |
+
|
201 |
with col1:
|
202 |
+
if st.button("Fetch Random Comic"):
|
203 |
+
with st.spinner("Fetching a random comic..."):
|
204 |
+
img, title, alt, url = random_comic.forward()
|
205 |
+
if img:
|
206 |
+
st.session_state.comic_fetched = True
|
207 |
+
st.session_state.comic_img = img
|
208 |
+
st.session_state.comic_title = title
|
209 |
+
st.session_state.comic_alt = alt
|
210 |
+
st.session_state.comic_url = url
|
211 |
+
st.experimental_rerun()
|
212 |
|
213 |
+
if st.session_state.comic_fetched:
|
214 |
+
with col1:
|
215 |
+
st.image(st.session_state.comic_img, caption=st.session_state.comic_title)
|
216 |
+
st.caption(f"Alt text: {st.session_state.comic_alt}")
|
217 |
+
st.markdown(f"[View on XKCD]({st.session_state.comic_url})")
|
218 |
|
219 |
+
with col2:
|
220 |
+
st.subheader("Ask AI about this comic")
|
221 |
+
query = st.text_input("What would you like to know about this comic?",
|
222 |
+
placeholder="Explain this comic in a funny way")
|
223 |
+
|
224 |
+
if st.button("Ask AI"):
|
225 |
+
with st.spinner("Generating response..."):
|
226 |
+
agent = CodeAgent(tools=[random_comic], model=st.session_state.model)
|
227 |
+
response = agent.run(query if query else "Tell me about this XKCD comic.")
|
228 |
+
st.markdown("### AI Response")
|
229 |
+
st.markdown(response)
|
230 |
|
231 |
+
elif tool_choice == "Prime Number Checker":
|
232 |
+
st.header("Prime Number Checker")
|
233 |
+
st.markdown("Check if a number is prime and get a creative explanation")
|
234 |
+
|
235 |
+
number = st.number_input("Enter a number to check:", min_value=1, step=1, value=23)
|
236 |
+
explanation_style = st.selectbox(
|
237 |
+
"Choose explanation style:",
|
238 |
+
["Poetic", "Nursery Rhyme", "Scientific", "Humorous", "Historical"]
|
239 |
+
)
|
240 |
+
|
241 |
+
if st.button("Check Prime"):
|
242 |
+
with st.spinner("Checking and generating response..."):
|
243 |
+
is_prime = prime_check_tool.forward(int(number))
|
244 |
+
|
245 |
+
# Format query based on selected style
|
246 |
+
query = f"Check if the number {number} is a prime number and explain it in a {explanation_style.lower()} style."
|
247 |
+
|
248 |
+
agent = CodeAgent(tools=[prime_check_tool], model=st.session_state.model)
|
249 |
+
response = agent.run(query)
|
250 |
+
|
251 |
+
# Display result with some styling
|
252 |
+
prime_status = "β
PRIME" if is_prime else "β NOT PRIME"
|
253 |
+
st.markdown(f"### Result: {prime_status}")
|
254 |
+
|
255 |
+
st.markdown("### AI Explanation")
|
256 |
+
st.markdown(response)
|
257 |
+
|
258 |
+
# Optional: Show a mathematical representation
|
259 |
+
if st.checkbox("Show mathematical details"):
|
260 |
+
if is_prime:
|
261 |
+
st.markdown(f"{number} has no factors other than 1 and itself.")
|
262 |
+
else:
|
263 |
+
# Find the factors
|
264 |
+
factors = [i for i in range(1, number + 1) if number % i == 0]
|
265 |
+
st.markdown(f"Factors of {number}: {', '.join(map(str, factors))}")
|
266 |
+
else:
|
267 |
+
# Token not provided
|
268 |
+
st.info("π Please enter your Hugging Face API token in the sidebar to get started.")
|
269 |
|
270 |
+
# Show more information to help users understand what they need
|
271 |
+
st.markdown("""
|
272 |
+
### How to Get a Hugging Face Token
|
|
|
|
|
273 |
|
274 |
+
1. Go to [huggingface.co](https://huggingface.co/) and create an account if you don't have one
|
275 |
+
2. Visit your [settings page](https://huggingface.co/settings/tokens)
|
276 |
+
3. Create a new token with read access
|
277 |
+
4. Copy the token and paste it in the sidebar
|
278 |
+
|
279 |
+
Your token will be used to access the language models needed for this application.
|
280 |
+
|
281 |
+
### Why is a token required?
|
282 |
+
|
283 |
+
This app uses Hugging Face's API to access powerful language models like Llama 3. Your API token
|
284 |
+
grants access to these models, which do the heavy lifting of understanding and responding to your queries.
|
285 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
286 |
|
287 |
# Add footer
|
288 |
st.sidebar.divider()
|