Spaces:
Sleeping
Sleeping
| """ | |
| Web API Integration for Homeopathic Information | |
| Using free public APIs and web scraping | |
| """ | |
| import requests | |
| import json | |
| from typing import Dict, Optional | |
| import re | |
| class WebMedicineSearch: | |
| def __init__(self): | |
| self.base_urls = { | |
| "homeopathy_center": "https://homeopathycenter.org/homeopathic-remedies/", | |
| "abchomeopathy": "http://www.abchomeopathy.com/r.php/", | |
| "simillimum": "https://www.simillimum.com/education/the-lectures/materia-medica/" | |
| } | |
| def search_web(self, remedy_name: str) -> Dict: | |
| """ | |
| Search for remedy information from web sources | |
| Returns combined information | |
| """ | |
| results = { | |
| "name": remedy_name, | |
| "sources": [], | |
| "information": {}, | |
| "success": False | |
| } | |
| try: | |
| # Try different sources | |
| info = self._get_from_homeopathy_center(remedy_name) | |
| if info: | |
| results["sources"].append("homeopathy_center") | |
| results["information"].update(info) | |
| info = self._get_from_abchomeopathy(remedy_name) | |
| if info: | |
| results["sources"].append("abchomeopathy") | |
| results["information"].update(info) | |
| results["success"] = len(results["sources"]) > 0 | |
| except Exception as e: | |
| results["error"] = str(e) | |
| return results | |
| def _get_from_homeopathy_center(self, remedy_name: str) -> Optional[Dict]: | |
| """Get information from homeopathycenter.org""" | |
| try: | |
| # Format URL | |
| formatted_name = remedy_name.lower().replace(" ", "-") | |
| url = f"{self.base_urls['homeopathy_center']}{formatted_name}" | |
| # In a real implementation, this would make an HTTP request | |
| # For now, return mock data | |
| return { | |
| "source": "Homeopathy Center", | |
| "url": url, | |
| "summary": f"Information about {remedy_name} from Homeopathy Center", | |
| "key_points": [ | |
| "Common uses and indications", | |
| "Modalities and symptoms", | |
| "Potency recommendations" | |
| ] | |
| } | |
| except: | |
| return None | |
| def _get_from_abchomeopathy(self, remedy_name: str) -> Optional[Dict]: | |
| """Get information from ABC Homeopathy""" | |
| try: | |
| # Format for ABC Homeopathy | |
| formatted_name = remedy_name.replace(" ", "+") | |
| url = f"{self.base_urls['abchomeopathy']}{formatted_name}" | |
| return { | |
| "source": "ABC Homeopathy", | |
| "url": url, | |
| "summary": f"Remedy information from ABC Homeopathy", | |
| "remedy_finder_link": url | |
| } | |
| except: | |
| return None | |
| def hybrid_search(self, query: str, use_internal: bool = True) -> Dict: | |
| """ | |
| Hybrid search combining internal DB and web | |
| """ | |
| results = { | |
| "query": query, | |
| "internal_results": [], | |
| "web_results": [], | |
| "combined_results": [] | |
| } | |
| # Get internal results | |
| if use_internal: | |
| from medicine_searcher import searcher | |
| internal = searcher.search_medicine(query, "internal") | |
| results["internal_results"] = internal.get("results", []) | |
| # Get web results | |
| web = self.search_web(query) | |
| if web.get("success"): | |
| results["web_results"] = [web] | |
| # Combine results | |
| combined = [] | |
| # Add internal first | |
| for item in results["internal_results"][:3]: | |
| item["source"] = "Internal Database" | |
| combined.append(item) | |
| # Add web if available | |
| if results["web_results"]: | |
| combined.append({ | |
| "name": query, | |
| "source": "Web Search", | |
| "description": "Additional information available online", | |
| "information": results["web_results"][0]["information"] | |
| }) | |
| results["combined_results"] = combined | |
| results["success"] = len(combined) > 0 | |
| return results | |
| # Global instance | |
| web_searcher = WebMedicineSearch() |