Spaces:
Sleeping
Sleeping
Upload 2 files
Browse filesFiles changed (2)
- app.py+15-15
- requirements.txt-1
6 unmodified lines7891011121314151 unmodified line171819202122232413 unmodified lines383940414243445 unmodified lines5051525354555657585960616263646566676869707172737475767778796 unmodified lineslogging.basicConfig(level=logging.INFO)logger = logging.getLogger(__name__)HF_TOKEN = os.environ.get("HF_TOKEN", "")HF_API_URL = "https://router.huggingface.co/v1/chat/completions"MODEL_ID = "meta-llama/Llama-3.1-8B-Instruct"@spaces.GPU(duration=60)1 unmodified lineif not email_text or not email_text.strip():return "Please enter email text."if not HF_TOKEN:return "Error: HF_TOKEN not configured. Add it as a Space secret named HF_TOKEN."email_text = email_text.strip()13 unmodified lines]headers = {"Authorization": f"Bearer {HF_TOKEN}","Content-Type": "application/json",}5 unmodified lines}try:logger.info("Calling HF Inference API")response = requests.post(HF_API_URL, headers=headers, json=payload, timeout=30)if response.status_code == 429:return "Error: Rate limited by HF API. Please try again in a moment."if response.status_code != 200:logger.error(f"HF API error {response.status_code}: {response.text[:500]}")return f"Error: HF Inference API returned status {response.status_code}"data = response.json()if "choices" not in data or len(data["choices"]) == 0:return "Error: HF API returned unexpected response"suggestion = data["choices"][0].get("message", {}).get("content", "").strip()if not suggestion:return "Error: HF API returned empty suggestion"return suggestionexcept requests.exceptions.Timeout:return "Error: HF API request timed out. Please try again."except requests.exceptions.ConnectionError:return "Error: Could not connect to HF API. Please try again later."except Exception as e:logger.error(f"Unexpected error: {e}")return f"Error: {str(e)}"6 unmodified lines7891011121314151 unmodified line171819202122232413 unmodified lines383940414243445 unmodified lines5051525354555657585960616263646566676869707172737475767778796 unmodified lineslogging.basicConfig(level=logging.INFO)logger = logging.getLogger(__name__)GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY", "")GEMINI_API_URL = "https://generativelanguage.googleapis.com/v1beta/openai/chat/completions"MODEL_ID = "gemini-2.5-flash"@spaces.GPU(duration=60)1 unmodified lineif not email_text or not email_text.strip():return "Please enter email text."if not GEMINI_API_KEY:return "Error: GEMINI_API_KEY not configured. Add it as a Space secret named GEMINI_API_KEY. Get a free key at https://aistudio.google.com/apikey"email_text = email_text.strip()13 unmodified lines]headers = {"Authorization": f"Bearer {GEMINI_API_KEY}","Content-Type": "application/json",}5 unmodified lines}try:logger.info("Calling Gemini API")response = requests.post(GEMINI_API_URL, headers=headers, json=payload, timeout=30)if response.status_code == 429:return "Error: Rate limited by Gemini API. Please try again in a moment."if response.status_code != 200:logger.error(f"Gemini API error {response.status_code}: {response.text[:500]}")return f"Error: Gemini API returned status {response.status_code}"data = response.json()if "choices" not in data or len(data["choices"]) == 0:return "Error: Gemini API returned unexpected response"suggestion = data["choices"][0].get("message", {}).get("content", "").strip()if not suggestion:return "Error: Gemini API returned empty suggestion"return suggestionexcept requests.exceptions.Timeout:return "Error: Gemini API request timed out. Please try again."except requests.exceptions.ConnectionError:return "Error: Could not connect to Gemini API. Please try again later."except Exception as e:logger.error(f"Unexpected error: {e}")return f"Error: {str(e)}"
123requestsgradiospaces12requestsgradio
app.py+15-15
requirements.txt-1