{"repo": "arka-siuu/sanjeevani", "files": {"healthcare_ai_agent/app.py": {"86a850bf90168e6d638f6bc968f990a5eabe7e9c": {"t": 1785674588.2876196, "n": 5966, "h": "e9a2e986475cb650", "c": "import os\r\nimport sys\r\nimport psutil\r\nimport asyncio\r\nimport re\r\nimport requests\r\nfrom bs4 import BeautifulSoup\r\nfrom typing import List\r\n\r\n# Gemini API Endpoint\r\nGEMINI_API_KEY = \"AIzaSyBxmj0zGRP-ndq2VKHjvh1q-XW3_Lrliq0\" # Replace with your Gemini API key\r\nGEMINI_API_URL = \"https://generativelanguage.googleapis.com/v1/models/gemini-pro:generateContent\"\r\n\r\n__location__ = os.path.dirname(os.path.abspath(__file__))\r\n__output__ = os.path.join(__location__, \"output\")\r\n\r\n# Append parent directory to system path\r\nparent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))\r\nsys.path.append(parent_dir)\r\n\r\n\r\nasync def fetch_page_content(url: str) -> str:\r\n \"\"\"Fetch the content of a webpage using REST (requests).\"\"\"\r\n headers = {\"User-Agent\": \"Mozilla/5.0\"}\r\n try:\r\n response = requests.get(url, headers=headers, timeout=10)\r\n response.raise_for_status()\r\n soup = BeautifulSoup(response.text, \"html.parser\")\r\n return soup.get_text()\r\n except requests.exceptions.RequestException as e:\r\n print(f\"Error fetching {url}: {e}\")\r\n return \"\"\r\n\r\n\r\nasync def crawl_parallel(urls: List[str], max_concurrent: int = 3):\r\n \"\"\"Crawl URLs in parallel and store results.\"\"\"\r\n print(\"\\n=== Parallel Crawling with REST ===\")\r\n\r\n peak_memory = 0\r\n process = psutil.Process(os.getpid())\r\n\r\n def log_memory(prefix: str = \"\"):\r\n \"\"\"Monitor and log memory usage.\"\"\"\r\n nonlocal peak_memory\r\n current_mem = process.memory_info().rss # in bytes\r\n if current_mem > peak_memory:\r\n peak_memory = current_mem\r\n print(f\"{prefix} Current Memory: {current_mem // (1024 * 1024)} MB, Peak: {peak_memory // (1024 * 1024)} MB\")\r\n\r\n messages = []\r\n url_pattern = re.compile(r'https?://\\S+|www\\.\\S+')\r\n\r\n success_count = 0\r\n fail_count = 0\r\n\r\n for i in range(0, len(urls), max_concurrent):\r\n batch = urls[i: i + max_concurrent]\r\n tasks = [fetch_page_content(url) for url in batch]\r\n\r\n log_memory(prefix=f\"Before batch {i // max_concurrent + 1}: \")\r\n results = await asyncio.gather(*tasks, return_exceptions=True)\r\n log_memory(prefix=f\"After batch {i // max_concurrent + 1}: \")\r\n\r\n for url, result in zip(batch, results):\r\n if isinstance(result, Exception) or not result:\r\n print(f\"Failed to crawl {url}\")\r\n fail_count += 1\r\n else:\r\n success_count += 1\r\n cleaned_content = url_pattern.sub(\"\", result)\r\n messages.append({\"source\": url, \"content\": cleaned_content})\r\n\r\n print(f\"\\nSummary:\\n - Successfully crawled: {success_count}\\n - Failed: {fail_count}\\n\")\r\n\r\n return messages\r\n\r\n\r\nasync def process_with_gemini(messages: List[dict]):\r\n \"\"\"Process scraped messages using Gemini REST API.\"\"\"\r\n llm_knowledge = []\r\n\r\n for message in messages:\r\n data = {\r\n \"contents\": [{\"parts\": [{\"text\": message[\"content\"]}]}]\r\n }\r\n params = {\"key\": GEMINI_API_KEY}\r\n\r\n try:\r\n response = requests.post(GEMINI_API_URL, json=data, params=params)\r\n response.raise_for_status()\r\n gemini_response = response.json()\r\n\r\n if \"candidates\" in gemini_response and gemini_response[\"candidates\"]:\r\n processed_text = gemini_response[\"candidates\"][0][\"content\"][\"parts\"][0][\"text\"]\r\n else:\r\n processed_text = \"No meaningful response from Gemini.\"\r\n\r\n llm_knowledge.append({\r\n \"source\": message[\"source\"],\r\n \"knowledge\": processed_text\r\n })\r\n\r\n except requests.exceptions.RequestException as e:\r\n print(f\"Error processing with Gemini: {e}\")\r\n\r\n return llm_knowledge\r\n\r\n\r\nasync def answer_user_query(user_query: str, knowledge_base: List[dict]):\r\n \"\"\"Finds the best solution for a user's query from scraped knowledge using Gemini.\"\"\"\r\n\r\n relevant_info = \"\\n\".join([f\"{entry['source']}:\\n{entry['knowledge']}\" for entry in knowledge_base])\r\n\r\n prompt = f\"\"\"\r\n The user has asked: \"{user_query}\"\r\n\r\n Below is the knowledge base extracted from trusted sources:\r\n {relevant_info}\r\n\r\n Please provide a personalized response to the user's query using the most relevant information.\r\n \"\"\"\r\n\r\n data = {\r\n \"contents\": [{\"parts\": [{\"text\": prompt}]}]\r\n }\r\n params = {\"key\": GEMINI_API_KEY}\r\n\r\n try:\r\n response = requests.post(GEMINI_API_URL, json=data, params=params)\r\n response.raise_for_status()\r\n gemini_response = response.json()\r\n\r\n if \"candidates\" in gemini_response and gemini_response[\"candidates\"]:\r\n answer = gemini_response[\"candidates\"][0][\"content\"][\"parts\"][0][\"text\"]\r\n else:\r\n answer = \"I'm sorry, but I couldn't find a suitable answer.\"\r\n\r\n return answer\r\n\r\n except requests.exceptions.RequestException as e:\r\n print(f\"Error fetching personalized solution: {e}\")\r\n return \"There was an error processing your request.\"\r\n\r\n\r\nasync def main():\r\n urls = [\r\n \"https://mohfw.gov.in/\",\r\n \"https://www.india.gov.in/topics/social-development/women\",\r\n \"https://wcd.gov.in/directory\",\r\n ]\r\n\r\n if urls:\r\n print(f\"Found {len(urls)} URLs to crawl\")\r\n messages = await crawl_parallel(urls, max_concurrent=10)\r\n\r\n llm_knowledge = await process_with_gemini(messages)\r\n\r\n print(\"\\nLLM Knowledge Processed. Now ready to answer user queries!\")\r\n\r\n # Take user query\r\n user_query = input(\"\\nEnter your question: \")\r\n personalized_response = await answer_user_query(user_query, llm_knowledge)\r\n\r\n print(\"\\n=== Personalized Solution ===\")\r\n print(personalized_response)\r\n\r\n return personalized_response\r\n else:\r\n print(\"No URLs found to crawl\")\r\n return []\r\n\r\n\r\nif __name__ == \"__main__\":\r\n asyncio.run(main())\r\n"}}}}