Download app.py from m-PAIRS/GST: direct link, hf CLI and curl.
- Browser
- Download file 2.39 kB
-
https://huggingface.co/spaces/m-PAIRS/GST/resolve/main/app.py
- Command line
-
hf download hf://spaces/m-PAIRS/GST/app.py
-
curl -L -o app.py https://huggingface.co/spaces/m-PAIRS/GST/resolve/main/app.py
2.39 kB
| import os | |
| import logging | |
| import httpx | |
| from flask import Flask, request, jsonify | |
| from flask_cors import CORS | |
| from openai import OpenAI | |
| # Configure logging | |
| logging.basicConfig(level=logging.INFO) | |
| logger = logging.getLogger(__name__) | |
| app = Flask(__name__) | |
| CORS(app) | |
| # --- Initialize Groq Endpoint via Proxy-Aware OpenAI Client --- | |
| groq_api_key = os.getenv("GROQ_API_KEY") | |
| if not groq_api_key: | |
| logger.warning("⚠️ Warning: GROQ_API_KEY environment secret is missing.") | |
| # 💡 FIX: Native integration with Hugging Face's global proxy layer | |
| http_client = httpx.Client( | |
| trust_env=True | |
| ) | |
| client_groq = OpenAI( | |
| api_key=groq_api_key, | |
| base_url="https://groq.com", | |
| timeout=60.0, | |
| max_retries=3, | |
| http_client=http_client | |
| ) | |
| logger.info("✅ Universal Proxy-Aware Client Initialized for Groq.") | |
| def health_check(): | |
| return jsonify({ | |
| "status": "online", | |
| "service": "mPAIRS GST Advisory Backend", | |
| "engine": "Groq (openai/gpt-oss-120b)" | |
| }), 200 | |
| def ask(): | |
| try: | |
| data = request.get_json() or {} | |
| user_prompt = data.get("query") or data.get("question") or data.get("prompt") or "" | |
| user_prompt = user_prompt.strip() | |
| if not user_prompt: | |
| return jsonify({"error": "Prompt cannot be empty."}), 400 | |
| logger.info("💬 Attempting generation via Groq [openai/gpt-oss-120b]...") | |
| chat_completion = client_groq.chat.completions.create( | |
| messages=[{"role": "user", "content": user_prompt}], | |
| model="openai/gpt-oss-120b", | |
| temperature=0.0 | |
| ) | |
| raw_answer = chat_completion.choices[0].message.content | |
| logger.info("✅ Successfully generated output from Groq endpoint.") | |
| return jsonify({ | |
| "answer": raw_answer, | |
| "status": "success", | |
| "model": "openai/gpt-oss-120b" | |
| }), 200 | |
| except Exception as e: | |
| error_msg = str(e) | |
| logger.error(f"❌ Execution failed! Error details: {error_msg}") | |
| return jsonify({ | |
| "error": "The AI Advisor encountered a connection issue. Please try again.", | |
| "details": error_msg | |
| }), 500 | |
| if __name__ == "__main__": | |
| port = int(os.environ.get("PORT", 7860)) | |
| app.run(host="0.0.0.0", port=port, debug=False) | |