Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from langgraph.graph import StateGraph, END
|
| 5 |
+
from typing import Dict
|
| 6 |
+
|
| 7 |
+
# =====================
|
| 8 |
+
# 1. SERP API Wrapper
|
| 9 |
+
# =====================
|
| 10 |
+
class SERPAPI:
|
| 11 |
+
def __init__(self, api_key: str):
|
| 12 |
+
self.api_key = api_key
|
| 13 |
+
self.endpoint = "https://serpapi.com/search.json"
|
| 14 |
+
|
| 15 |
+
def search(self, query: str) -> str:
|
| 16 |
+
"""Fetch Google search results using SERP API"""
|
| 17 |
+
params = {
|
| 18 |
+
"engine": "google",
|
| 19 |
+
"q": query,
|
| 20 |
+
"hl": "en",
|
| 21 |
+
"api_key": self.api_key
|
| 22 |
+
}
|
| 23 |
+
try:
|
| 24 |
+
response = requests.get(self.endpoint, params=params)
|
| 25 |
+
response.raise_for_status()
|
| 26 |
+
data = response.json()
|
| 27 |
+
snippets = [item.get("snippet", "") for item in data.get("organic_results", [])[:5]]
|
| 28 |
+
return "\n".join(snippets) if snippets else "No information found."
|
| 29 |
+
except Exception as e:
|
| 30 |
+
return f"[SERP API Error]: {str(e)}"
|
| 31 |
+
|
| 32 |
+
# =====================
|
| 33 |
+
# 2. Dummy LLM (simulates Gemini API)
|
| 34 |
+
# =====================
|
| 35 |
+
class DummyLLM:
|
| 36 |
+
def summarize(self, text: str, instruction: str) -> str:
|
| 37 |
+
"""Simulate explanation / summarization"""
|
| 38 |
+
return f"[Tutor Explanation]\nInstruction: {instruction}\nContent: {text[:500]}..."
|
| 39 |
+
|
| 40 |
+
# =====================
|
| 41 |
+
# 3. Shared Agent State
|
| 42 |
+
# =====================
|
| 43 |
+
class AgentState(dict):
|
| 44 |
+
user_query: str
|
| 45 |
+
discovery: str
|
| 46 |
+
advice: str
|
| 47 |
+
usage_plan: str
|
| 48 |
+
cost_guide: str
|
| 49 |
+
roadmap: str
|
| 50 |
+
|
| 51 |
+
# =====================
|
| 52 |
+
# 4. Initialize APIs
|
| 53 |
+
# =====================
|
| 54 |
+
SERP_API_KEY = "d2819e2c47904d266f01b212b44ab6e9906df363097f7d167e3427e95c7c31a6"
|
| 55 |
+
serp_api = SERPAPI(api_key=SERP_API_KEY)
|
| 56 |
+
gemini = DummyLLM()
|
| 57 |
+
|
| 58 |
+
# =====================
|
| 59 |
+
# 5. Define Agents
|
| 60 |
+
# =====================
|
| 61 |
+
def discovery_agent(state: AgentState):
|
| 62 |
+
search_results = serp_api.search(f"AWS {state['user_query']} overview features beginner advanced")
|
| 63 |
+
prompt = f"Explain the purpose, overview, main features, and usage of these AWS tools for beginners and advanced users:\n{search_results}"
|
| 64 |
+
state['discovery'] = gemini.summarize(search_results, prompt)
|
| 65 |
+
return state
|
| 66 |
+
|
| 67 |
+
def advisor_agent(state: AgentState):
|
| 68 |
+
prompt = f"Advise which AWS tools are most useful and why. Discovery info: {state['discovery']}"
|
| 69 |
+
state['advice'] = gemini.summarize(state['discovery'], prompt)
|
| 70 |
+
return state
|
| 71 |
+
|
| 72 |
+
def usage_planner_agent(state: AgentState):
|
| 73 |
+
prompt = f"Explain AWS Free Tier limits, usage patterns, and best practices: {state['advice']}"
|
| 74 |
+
state['usage_plan'] = gemini.summarize(state['advice'], prompt)
|
| 75 |
+
return state
|
| 76 |
+
|
| 77 |
+
def cost_agent(state: AgentState):
|
| 78 |
+
prompt = f"Provide cost guide and optimization tips for: {state['advice']}"
|
| 79 |
+
state['cost_guide'] = gemini.summarize(state['advice'], prompt)
|
| 80 |
+
return state
|
| 81 |
+
|
| 82 |
+
def roadmap_agent(state: AgentState):
|
| 83 |
+
prompt = f"Create a step-by-step AWS learning roadmap for beginners and advanced users. Tools: {state['advice']}"
|
| 84 |
+
state['roadmap'] = gemini.summarize(state['advice'], prompt)
|
| 85 |
+
return state
|
| 86 |
+
|
| 87 |
+
# =====================
|
| 88 |
+
# 6. Build Multi-Agent Graph
|
| 89 |
+
# =====================
|
| 90 |
+
workflow = StateGraph(AgentState)
|
| 91 |
+
workflow.add_node('discovery', discovery_agent)
|
| 92 |
+
workflow.add_node('advisor', advisor_agent)
|
| 93 |
+
workflow.add_node('usage', usage_planner_agent)
|
| 94 |
+
workflow.add_node('cost', cost_agent)
|
| 95 |
+
workflow.add_node('roadmap', roadmap_agent)
|
| 96 |
+
workflow.set_entry_point('discovery')
|
| 97 |
+
workflow.add_edge('discovery', 'advisor')
|
| 98 |
+
workflow.add_edge('advisor', 'usage')
|
| 99 |
+
workflow.add_edge('usage', 'cost')
|
| 100 |
+
workflow.add_edge('cost', 'roadmap')
|
| 101 |
+
workflow.add_edge('roadmap', END)
|
| 102 |
+
app_graph = workflow.compile()
|
| 103 |
+
|
| 104 |
+
# =====================
|
| 105 |
+
# 7. Gradio Interface
|
| 106 |
+
# =====================
|
| 107 |
+
def aws_tutor(user_input):
|
| 108 |
+
result = app_graph.invoke({'user_query': user_input})
|
| 109 |
+
return (result['discovery'], result['advice'], result['usage_plan'], result['cost_guide'], result['roadmap'])
|
| 110 |
+
|
| 111 |
+
iface = gr.Interface(
|
| 112 |
+
fn=aws_tutor,
|
| 113 |
+
inputs=gr.Textbox(label="Enter your AWS learning goal or tool"),
|
| 114 |
+
outputs=[
|
| 115 |
+
gr.Textbox(label="Discovery (Overview)"),
|
| 116 |
+
gr.Textbox(label="Advice (Recommended Tools)"),
|
| 117 |
+
gr.Textbox(label="Usage Plan (Free Tier + Tips)"),
|
| 118 |
+
gr.Textbox(label="Cost & Optimization Guide"),
|
| 119 |
+
gr.Textbox(label="Roadmap (Step-by-Step Learning Plan)")
|
| 120 |
+
],
|
| 121 |
+
title="AWS Tutor",
|
| 122 |
+
description="A multi-agent AWS tutor that provides discovery, advice, usage guidance, cost optimization, and roadmap for learning AWS tools."
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
if __name__ == "__main__":
|
| 126 |
+
iface.launch()
|