Spaces:
Runtime error
Runtime error
File size: 1,061 Bytes
64b4650 eb69519 64b4650 eb69519 64b4650 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
import os
import openai
from flask import Flask, request, jsonify
app = Flask(__name__)
# β
Get the API key from environment variables
openai.api_key = os.getenv("OPENAI_API_KEY")
if not openai.api_key:
raise ValueError("β OPENAI_API_KEY is not set. Go to Hugging Face > Settings > Variables and Secrets and add it there.")
@app.route("/chat", methods=["POST"])
def chat():
try:
data = request.json
user_message = data.get("message", "")
if not user_message:
return jsonify({"error": "Message is required"}), 400
# Example: call the ChatGPT model
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo", # or gpt-4 if available
messages=[{"role": "user", "content": user_message}],
temperature=0.7
)
reply = response.choices[0].message["content"]
return jsonify({"reply": reply})
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860)
|