/ web_ai.py
web_ai.py
1 #!/usr/bin/env python3 2 """ 3 WEB-BASED AI - NO API KEYS NEEDED 4 """ 5 6 import requests 7 import json 8 import sys 9 import re 10 from bs4 import BeautifulSoup 11 import urllib.parse 12 13 def search_duckduckgo(query): 14 """Search DuckDuckGo for answers""" 15 url = f"https://html.duckduckgo.com/html/?q={urllib.parse.quote(query)}" 16 headers = { 17 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36' 18 } 19 20 try: 21 response = requests.get(url, headers=headers, timeout=10) 22 soup = BeautifulSoup(response.text, 'html.parser') 23 24 # Extract snippets 25 results = [] 26 for result in soup.find_all('a', class_='result__snippet'): 27 text = result.get_text(strip=True) 28 if text and len(text) > 50: 29 results.append(text[:300]) 30 31 return " | ".join(results[:3]) if results else "No results found" 32 except: 33 return "Search failed" 34 35 def get_wikipedia_summary(query): 36 """Get Wikipedia summary""" 37 try: 38 url = f"https://en.wikipedia.org/api/rest_v1/page/summary/{urllib.parse.quote(query)}" 39 response = requests.get(url, timeout=10) 40 data = response.json() 41 return data.get('extract', 'No summary available') 42 except: 43 return "Wikipedia unavailable" 44 45 def get_ai_response_from_web(query): 46 """Combine web sources for AI-like response""" 47 48 # Simple rule-based responses for common questions 49 responses = { 50 r'what is (ai|artificial intelligence)': "Artificial Intelligence is the simulation of human intelligence in machines that are programmed to think and learn.", 51 r'what is machine learning': "Machine learning is a subset of AI that enables computers to learn and improve from experience without being explicitly programmed.", 52 r'capital of france': "The capital of France is Paris.", 53 r'hello|hi|hey': "Hello! I'm an AI assistant. How can I help you today?", 54 r'how are you': "I'm a computer program, so I don't have feelings, but I'm functioning correctly! How can I assist you?", 55 r'your name': "I'm an AI assistant created to help answer questions.", 56 r'thank you': "You're welcome! Is there anything else I can help with?", 57 } 58 59 # Check for matching patterns 60 query_lower = query.lower() 61 for pattern, response in responses.items(): 62 if re.search(pattern, query_lower): 63 return response 64 65 # Fallback to web search 66 ddg_result = search_duckduckgo(query) 67 wiki_result = get_wikipedia_summary(query) 68 69 if wiki_result != "Wikipedia unavailable": 70 return f"Based on Wikipedia: {wiki_result[:400]}..." 71 elif "No results" not in ddg_result: 72 return f"Based on web search: {ddg_result[:400]}..." 73 else: 74 return f"I don't have a specific answer for '{query}', but I can tell you it's an interesting topic worth researching!" 75 76 def main(): 77 print("š WEB AI ASSISTANT - NO API KEYS") 78 print("="*50) 79 80 if len(sys.argv) > 1: 81 question = " ".join(sys.argv[1:]) 82 else: 83 print("\nAsk me anything:") 84 question = sys.stdin.readline().strip() or "What is AI?" 85 86 print(f"\nš¤ Question: {question}") 87 print("\nš Searching for answers...") 88 89 response = get_ai_response_from_web(question) 90 91 print("\nš Answer:") 92 print("="*40) 93 print(response) 94 print("="*40) 95 96 # Save to file 97 import datetime 98 timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S") 99 with open(f"web_ai_{timestamp}.txt", "w") as f: 100 f.write(f"Question: {question}\n") 101 f.write(f"Time: {datetime.datetime.now()}\n") 102 f.write(f"Answer: {response}\n") 103 104 print(f"\nš¾ Saved to: web_ai_{timestamp}.txt") 105 106 if __name__ == "__main__": 107 main()