Spaces:
Sleeping
Sleeping
File size: 3,183 Bytes
e4deca5 e5d7d06 9622206 e4deca5 9622206 e4deca5 9622206 e5d7d06 9622206 e4deca5 9622206 e4deca5 9622206 e4deca5 9622206 e4deca5 9622206 e5d7d06 9622206 e4deca5 9622206 e5d7d06 9622206 e4deca5 9622206 e4deca5 9622206 e4deca5 9622206 e4deca5 9622206 e4deca5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | import requests
import gradio as gr
# -------------------------------
# 1. SERP API for web search
# -------------------------------
class SERPAPI:
def __init__(self, api_key: str):
self.api_key = api_key
self.endpoint = "https://serpapi.com/search.json"
def search(self, query: str) -> str:
"""Fetch Google search results using SERP API"""
params = {
"engine": "google",
"q": query,
"hl": "en",
"api_key": self.api_key
}
try:
response = requests.get(self.endpoint, params=params)
response.raise_for_status()
data = response.json()
snippets = [item.get("snippet", "") for item in data.get("organic_results", [])[:5]]
return "\n".join(snippets) if snippets else "No information found."
except Exception as e:
return f"[SERP API Error]: {str(e)}"
# -------------------------------
# 2. Dummy LLM for summarization
# -------------------------------
class DummyLLM:
def summarize(self, text: str, instruction: str) -> str:
"""Simulate summarization or explanation"""
return f"[Tutor Explanation]\nInstruction: {instruction}\n\nSummary:\n{text[:500]}..."
# -------------------------------
# 3. AWS Tutor Logic
# -------------------------------
SERP_API_KEY = "d2819e2c47904d266f01b212b44ab6e9906df363097f7d167e3427e95c7c31a6"
serp_api = SERPAPI(api_key=SERP_API_KEY)
gemini = DummyLLM()
def aws_tutor(user_query: str):
"""Main tutor logic"""
# Step 1: Discovery
search_results = serp_api.search(f"AWS {user_query} overview features beginner advanced")
discovery = gemini.summarize(search_results, "Explain AWS service overview and features.")
# Step 2: Advice
advice = gemini.summarize(discovery, "Advise the most useful AWS tools for this topic.")
# Step 3: Usage Plan
usage_plan = gemini.summarize(advice, "Explain AWS Free Tier limits and best practices.")
# Step 4: Cost Guide
cost_guide = gemini.summarize(advice, "Provide AWS cost optimization tips.")
# Step 5: Roadmap
roadmap = gemini.summarize(advice, "Create a step-by-step AWS learning roadmap.")
return (
discovery,
advice,
usage_plan,
cost_guide,
roadmap
)
# -------------------------------
# 4. Gradio Interface
# -------------------------------
iface = gr.Interface(
fn=aws_tutor,
inputs=gr.Textbox(label="Enter your AWS learning goal or tool", placeholder="Example: EC2, S3, Lambda, or AI Services"),
outputs=[
gr.Textbox(label="Discovery (Overview)"),
gr.Textbox(label="Advice (Recommended Tools)"),
gr.Textbox(label="Usage Plan (Free Tier + Tips)"),
gr.Textbox(label="Cost & Optimization Guide"),
gr.Textbox(label="Roadmap (Step-by-Step Learning Plan)")
],
title="AWS Tutor",
description="An interactive AWS Tutor that provides discovery, advice, usage plan, cost guide, and roadmap for your AWS learning goal."
)
# -------------------------------
# 5. Launch App
# -------------------------------
if __name__ == "__main__":
iface.launch()
|