Spaces:
Sleeping
Sleeping
Download app.py from sujithrajaR/awsTutor: direct link, hf CLI and curl.
- Browser
- Download file 3.18 kB
-
https://huggingface.co/spaces/sujithrajaR/awsTutor/resolve/main/app.py
- Command line
-
hf download hf://spaces/sujithrajaR/awsTutor/app.py
-
curl -L -o app.py https://huggingface.co/spaces/sujithrajaR/awsTutor/resolve/main/app.py
3.18 kB
| 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() | |