Spaces:
Runtime error
Runtime error
Removed exposed API key
Browse files- example.py +32 -0
example.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
from flask import Flask, render_template, request, jsonify
|
| 3 |
+
import openai
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
app = Flask(__name__)
|
| 7 |
+
|
| 8 |
+
# ✅ Add your OpenAI API key here (or use environment variables)
|
| 9 |
+
openai.api_key = "OPENAI_API_KEY"
|
| 10 |
+
|
| 11 |
+
@app.route('/')
|
| 12 |
+
def home():
|
| 13 |
+
return render_template('index.html')
|
| 14 |
+
|
| 15 |
+
@app.route('/chat', methods=['POST'])
|
| 16 |
+
def chat():
|
| 17 |
+
user_input = request.json['user_input']
|
| 18 |
+
|
| 19 |
+
try:
|
| 20 |
+
response = openai.ChatCompletion.create(
|
| 21 |
+
model="gpt-4", # Change to "gpt-3.5-turbo" if needed
|
| 22 |
+
messages=[{"role": "system", "content": "You are a helpful AI chef."},
|
| 23 |
+
{"role": "user", "content": user_input}]
|
| 24 |
+
)
|
| 25 |
+
ai_response = response['choices'][0]['message']['content']
|
| 26 |
+
except Exception as e:
|
| 27 |
+
ai_response = f"Error: {str(e)}"
|
| 28 |
+
|
| 29 |
+
return jsonify({'ai_response': ai_response})
|
| 30 |
+
|
| 31 |
+
if __name__ == '__main__':
|
| 32 |
+
app.run(host="0.0.0.0", port=7860)
|