Spaces:
Sleeping
Sleeping
| import os, time, json, base64, requests | |
| from bs4 import BeautifulSoup | |
| from flask import Flask, request, jsonify, Response, stream_with_context | |
| from flask_cors import CORS | |
| app = Flask(__name__) | |
| CORS(app) | |
| def scrape_startpage(query, n=10): | |
| s = requests.Session() | |
| s.headers.update({ | |
| 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)', | |
| 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', | |
| 'Accept-Language': 'en-US,en;q=0.5', | |
| 'Accept-Encoding': 'gzip, deflate', | |
| 'Connection': 'keep-alive' | |
| }) | |
| try: | |
| time.sleep(1) | |
| r = s.get('https://www.startpage.com/sp/search', params={'query': query, 'cat': 'web', 'pl': 'opensearch'}) | |
| r.raise_for_status() | |
| soup = BeautifulSoup(r.content, 'html.parser') | |
| results = [] | |
| for c in soup.find_all('div', class_='result')[:n]: | |
| t = c.find('a', class_='result-title') | |
| if not t: continue | |
| d = c.find('p', class_='result-description') or c.find('span', class_='result-description') | |
| results.append({'title': t.get_text(strip=True), 'url': t.get('href'), 'desc': d.get_text(strip=True) if d else ''}) | |
| return results | |
| except Exception: | |
| return [] | |
| def get_weather_from_ip(ip_address): | |
| try: | |
| ip_data = requests.get(f'https://ipinfo.io/{ip_address}/json').json() | |
| loc = ip_data.get('loc') | |
| if not loc: return {'error': 'No location'} | |
| lat, lon = loc.split(',') | |
| if ip_data.get('country') != 'US': | |
| return {'error': 'Weather only available in US'} | |
| forecast = requests.get(requests.get(f'https://api.weather.gov/points/{lat},{lon}').json()['properties']['forecast']).json() | |
| current = forecast['properties']['periods'][0] | |
| return { | |
| 'ip': ip_address, | |
| 'location': {'city': ip_data.get('city', 'Unknown'), 'country': ip_data.get('country'), 'latitude': lat, 'longitude': lon}, | |
| 'weather': {'temperature': current['temperature'], 'description': current['detailedForecast']} | |
| } | |
| except Exception as e: | |
| return {'error': str(e)} | |
| def search(): | |
| q = request.args.get('q') | |
| if not q: return jsonify({'error': 'Missing query'}), 400 | |
| n = request.args.get('n', default=10, type=int) | |
| return jsonify({'query': q, 'results': scrape_startpage(q, n)}) | |
| def weather(): | |
| ip = request.args.get('ip') | |
| if not ip: return jsonify({'error': 'Missing ip'}), 400 | |
| data = get_weather_from_ip(ip) | |
| if 'error' in data: return jsonify(data), 400 | |
| return jsonify(data) | |
| def health(): | |
| return jsonify({'status': 'running', 'message': 'GridLock search API'}) | |
| def morsify_key(): | |
| prefix = "nvapi-" | |
| rest_enc = "zAPHnOBRkqd0ZogQ4h4lZh4c8lZ4LgOcOOlhLL" | |
| shift = 8 | |
| decoded = ''.join(chr((ord(c)-shift) % 126) for c in rest_enc) | |
| return prefix + decoded | |
| def chat_completions(): | |
| data = request.get_json(force=True) | |
| if not data or 'messages' not in data: | |
| return jsonify({'error': 'Missing messages'}), 400 | |
| stream = data.get('stream', True) | |
| api_key = morsify_key() | |
| print(f"[DEBUG] Using API key: {api_key}") # <-- logs reconstructed key to console | |
| payload = { | |
| "model": data.get("model", "meta/llama-4-scout-17b-16e-instruct"), | |
| "messages": data["messages"], | |
| "max_tokens": data.get("max_tokens", 512), | |
| "temperature": data.get("temperature", 1.0), | |
| "top_p": data.get("top_p", 1.0), | |
| "frequency_penalty": data.get("frequency_penalty", 0.0), | |
| "presence_penalty": data.get("presence_penalty", 0.0), | |
| "stream": stream | |
| } | |
| headers = {"Authorization": f"Bearer {api_key}", "Accept": "text/event-stream" if stream else "application/json"} | |
| try: | |
| r = requests.post("https://integrate.api.nvidia.com/v1/chat/completions", headers=headers, json=payload, stream=stream, timeout=120) | |
| if stream: | |
| def generate(): | |
| for line in r.iter_lines(): | |
| if line: yield f"data: {line.decode()}\n\n" | |
| return Response(stream_with_context(generate()), content_type='text/event-stream') | |
| else: | |
| return jsonify(r.json()) | |
| except Exception as e: | |
| return jsonify({'error': str(e)}), 500 | |
| if __name__ == '__main__': | |
| app.run(host='0.0.0.0', port=7860, debug=True) |