Update app.py
Browse files
app.py
CHANGED
@@ -225,6 +225,84 @@ def calculate_cargo_travel_time(origin_coords: Tuple[float, float], destination_
|
|
225 |
flight_time = (actual_distance / cruising_speed_kmh) + 1.0
|
226 |
return round(flight_time, 2)
|
227 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
228 |
# Main App
|
229 |
st.title("SFT Tiny Titans 🚀 (Small but Mighty!)")
|
230 |
|
@@ -387,43 +465,31 @@ if st.button("Generate"):
|
|
387 |
|
388 |
with tab4:
|
389 |
st.header("Agentic RAG Party 🌐 (Party Like It’s 2099!)")
|
390 |
-
st.write("This demo uses
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
391 |
|
392 |
-
if st.button("Run Agentic RAG Demo 🎉"):
|
393 |
-
try:
|
394 |
-
from smolagents import CodeAgent, DuckDuckGoSearchTool, VisitWebpageTool
|
395 |
-
from transformers import AutoModelForCausalLM
|
396 |
-
|
397 |
-
# Load the model without separate tokenizer for agent
|
398 |
-
with st.spinner("Loading SmolLM-135M... ⏳ (Titan’s suiting up!)"):
|
399 |
-
model = AutoModelForCausalLM.from_pretrained("HuggingFaceTB/SmolLM-135M")
|
400 |
-
st.write("Model loaded! 🦸♂️ (Ready to party!)")
|
401 |
-
|
402 |
-
# Initialize agent without tokenizer argument
|
403 |
-
agent = CodeAgent(
|
404 |
-
model=model,
|
405 |
-
tools=[DuckDuckGoSearchTool(), VisitWebpageTool(), calculate_cargo_travel_time],
|
406 |
-
additional_authorized_imports=["pandas"],
|
407 |
-
planning_interval=5,
|
408 |
-
verbosity_level=2,
|
409 |
-
max_steps=15,
|
410 |
-
)
|
411 |
-
|
412 |
task = """
|
413 |
-
Plan a luxury superhero-themed party at Wayne Manor (42.3601° N, 71.0589° W).
|
414 |
-
|
415 |
-
(
|
416 |
-
|
417 |
-
|
418 |
-
"""
|
419 |
with st.spinner("Planning the ultimate superhero bash... ⏳ (Calling all caped crusaders!)"):
|
420 |
-
|
421 |
-
|
422 |
-
|
423 |
-
|
424 |
-
|
425 |
-
|
426 |
-
|
427 |
-
st.error(f"Agent setup failed: {str(e)} (Looks like the Titans need a tune-up!)")
|
428 |
-
except Exception as e:
|
429 |
-
st.error(f"Error running demo: {str(e)} (Even Batman has off days!)")
|
|
|
225 |
flight_time = (actual_distance / cruising_speed_kmh) + 1.0
|
226 |
return round(flight_time, 2)
|
227 |
|
228 |
+
# Mock Search Tool for RAG
|
229 |
+
def mock_duckduckgo_search(query: str) -> str:
|
230 |
+
"""Simulate a search result for luxury superhero party trends"""
|
231 |
+
if "superhero party trends" in query.lower():
|
232 |
+
return """
|
233 |
+
Latest trends for 2025:
|
234 |
+
- Luxury decorations: Gold-plated Batman statues, holographic Avengers displays.
|
235 |
+
- Entertainment: Live stunt shows with Iron Man suits, VR superhero battles.
|
236 |
+
- Catering: Gourmet kryptonite-green cocktails, Thor’s hammer-shaped appetizers.
|
237 |
+
"""
|
238 |
+
return "No relevant results found."
|
239 |
+
|
240 |
+
# Simple Agent Class for Demo
|
241 |
+
class PartyPlannerAgent:
|
242 |
+
def __init__(self, model, tokenizer):
|
243 |
+
self.model = model
|
244 |
+
self.tokenizer = tokenizer
|
245 |
+
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
246 |
+
self.model.to(self.device)
|
247 |
+
|
248 |
+
def generate(self, prompt: str) -> str:
|
249 |
+
self.model.eval()
|
250 |
+
with torch.no_grad():
|
251 |
+
inputs = self.tokenizer(prompt, return_tensors="pt", max_length=128, truncation=True).to(self.device)
|
252 |
+
outputs = self.model.generate(
|
253 |
+
**inputs,
|
254 |
+
max_new_tokens=100,
|
255 |
+
do_sample=True,
|
256 |
+
top_p=0.95,
|
257 |
+
temperature=0.7
|
258 |
+
)
|
259 |
+
return self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
260 |
+
|
261 |
+
def plan_party(self, task: str) -> pd.DataFrame:
|
262 |
+
# Mock search for context
|
263 |
+
search_result = mock_duckduckgo_search("latest superhero party trends")
|
264 |
+
|
265 |
+
# Locations and coordinates
|
266 |
+
locations = {
|
267 |
+
"Wayne Manor": (42.3601, -71.0589),
|
268 |
+
"New York": (40.7128, -74.0060),
|
269 |
+
"Los Angeles": (34.0522, -118.2437),
|
270 |
+
"London": (51.5074, -0.1278)
|
271 |
+
}
|
272 |
+
|
273 |
+
# Calculate travel times
|
274 |
+
wayne_coords = locations["Wayne Manor"]
|
275 |
+
travel_times = {
|
276 |
+
loc: calculate_cargo_travel_time(coords, wayne_coords)
|
277 |
+
for loc, coords in locations.items() if loc != "Wayne Manor"
|
278 |
+
}
|
279 |
+
|
280 |
+
# Generate luxury ideas with the SFT model
|
281 |
+
prompt = f"""
|
282 |
+
Given this context from a search: "{search_result}"
|
283 |
+
Plan a luxury superhero-themed party at Wayne Manor. Suggest luxury decorations, entertainment, and catering ideas.
|
284 |
+
"""
|
285 |
+
plan_text = self.generate(prompt)
|
286 |
+
|
287 |
+
# Parse plan into structured data (simplified)
|
288 |
+
catchphrases = [
|
289 |
+
"To the Batmobile!",
|
290 |
+
"Avengers, assemble!",
|
291 |
+
"I am Iron Man!",
|
292 |
+
"By the power of Grayskull!"
|
293 |
+
]
|
294 |
+
|
295 |
+
data = [
|
296 |
+
{"Location": "New York", "Travel Time (hrs)": travel_times["New York"], "Luxury Idea": "Gold-plated Batman statues", "Catchphrase": random.choice(catchphrases)},
|
297 |
+
{"Location": "Los Angeles", "Travel Time (hrs)": travel_times["Los Angeles"], "Luxury Idea": "Holographic Avengers displays", "Catchphrase": random.choice(catchphrases)},
|
298 |
+
{"Location": "London", "Travel Time (hrs)": travel_times["London"], "Luxury Idea": "Live stunt shows with Iron Man suits", "Catchphrase": random.choice(catchphrases)},
|
299 |
+
{"Location": "Wayne Manor", "Travel Time (hrs)": 0.0, "Luxury Idea": "VR superhero battles", "Catchphrase": random.choice(catchphrases)},
|
300 |
+
{"Location": "New York", "Travel Time (hrs)": travel_times["New York"], "Luxury Idea": "Gourmet kryptonite-green cocktails", "Catchphrase": random.choice(catchphrases)},
|
301 |
+
{"Location": "Los Angeles", "Travel Time (hrs)": travel_times["Los Angeles"], "Luxury Idea": "Thor’s hammer-shaped appetizers", "Catchphrase": random.choice(catchphrases)},
|
302 |
+
]
|
303 |
+
|
304 |
+
return pd.DataFrame(data)
|
305 |
+
|
306 |
# Main App
|
307 |
st.title("SFT Tiny Titans 🚀 (Small but Mighty!)")
|
308 |
|
|
|
465 |
|
466 |
with tab4:
|
467 |
st.header("Agentic RAG Party 🌐 (Party Like It’s 2099!)")
|
468 |
+
st.write("This demo uses your SFT-tuned Tiny Titan to plan a superhero party with mock retrieval!")
|
469 |
+
|
470 |
+
if 'builder' not in st.session_state or not st.session_state.get('model_loaded', False):
|
471 |
+
st.warning("Please build or load a Titan first! ⚠️ (No Titan, no party!)")
|
472 |
+
else:
|
473 |
+
if st.button("Run Agentic RAG Demo 🎉"):
|
474 |
+
with st.spinner("Loading your SFT-tuned Titan... ⏳ (Titan’s suiting up!)"):
|
475 |
+
agent = PartyPlannerAgent(
|
476 |
+
model=st.session_state['builder'].model,
|
477 |
+
tokenizer=st.session_state['builder'].tokenizer
|
478 |
+
)
|
479 |
+
st.write("Agent ready! 🦸♂️ (Time to plan an epic bash!)")
|
480 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
481 |
task = """
|
482 |
+
Plan a luxury superhero-themed party at Wayne Manor (42.3601° N, 71.0589° W).
|
483 |
+
Use mock search results for the latest superhero party trends, refine for luxury elements
|
484 |
+
(decorations, entertainment, catering), and calculate cargo travel times from key locations
|
485 |
+
(New York: 40.7128° N, 74.0060° W; LA: 34.0522° N, 118.2437° W; London: 51.5074° N, 0.1278° W)
|
486 |
+
to Wayne Manor. Create a plan with at least 6 entries in a pandas dataframe.
|
487 |
+
"""
|
488 |
with st.spinner("Planning the ultimate superhero bash... ⏳ (Calling all caped crusaders!)"):
|
489 |
+
try:
|
490 |
+
plan_df = agent.plan_party(task)
|
491 |
+
st.write("Agentic RAG Party Plan:")
|
492 |
+
st.dataframe(plan_df)
|
493 |
+
st.write("Party on, Wayne! 🦸♂️🎉")
|
494 |
+
except Exception as e:
|
495 |
+
st.error(f"Error planning party: {str(e)} (Even Superman has kryptonite days!)")
|
|
|
|
|
|