Spaces:
Sleeping
Sleeping
| from flask import Flask, request, jsonify | |
| import openai | |
| app = Flask(__name__) | |
| import os | |
| openai.api_key = os.getenv("OPENAI_API_KEY") | |
| def home(): | |
| return jsonify({"message": "Flask API is running on Hugging Face!"}) | |
| def chat(): | |
| try: | |
| data = request.get_json() | |
| if not data or "prompt" not in data: | |
| return jsonify({"error": "Missing 'prompt' in request"}), 400 | |
| prompt = data["prompt"] | |
| # ✅ Use the new OpenAI API syntax | |
| response = openai.chat.completions.create( | |
| model="gpt-3.5-turbo", # Ensure correct model name | |
| messages=[{"role": "user", "content": prompt}] | |
| ) | |
| return jsonify({"response": response.choices[0].message.content}) | |
| except Exception as e: | |
| return jsonify({"error": str(e)}), 500 | |
| if __name__ == "__main__": | |
| app.run(host="0.0.0.0", port=7860) | |