Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,27 +1,60 @@
|
|
| 1 |
from flask import Flask, request, jsonify
|
| 2 |
-
|
| 3 |
import os
|
| 4 |
-
from flask_cors import CORS
|
| 5 |
-
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
-
openai_client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 9 |
-
CORS(app)
|
| 10 |
-
assistant_id = "asst_R7FO44rNY25nJeSszxHktjAR" # Replace with your actual Assistant ID
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
def chat():
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
while run.status != "completed":
|
| 22 |
-
run = openai_client.beta.threads.runs.retrieve(thread_id=thread.id, run_id=run.id)
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
|
| 27 |
-
return jsonify({"reply": bot_reply})
|
|
|
|
| 1 |
from flask import Flask, request, jsonify
|
| 2 |
+
import openai
|
| 3 |
import os
|
|
|
|
|
|
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# Set your OpenAI API key
|
| 8 |
+
openai.api_key = "sk-..." # Replace with your actual key
|
| 9 |
+
|
| 10 |
+
# Your Assistant ID (from platform.openai.com)
|
| 11 |
+
ASSISTANT_ID = "asst_izF6HxamgRWJf8RnMf7tdoMA"
|
| 12 |
+
|
| 13 |
+
# Store thread IDs per session/user if needed
|
| 14 |
+
thread_store = {}
|
| 15 |
+
|
| 16 |
+
@app.route("/chat", methods=["POST"])
|
| 17 |
def chat():
|
| 18 |
+
user_input = request.json.get("message")
|
| 19 |
+
user_id = request.json.get("user_id", "default")
|
| 20 |
+
|
| 21 |
+
# Create a thread if not exists
|
| 22 |
+
if user_id not in thread_store:
|
| 23 |
+
thread = openai.beta.threads.create()
|
| 24 |
+
thread_store[user_id] = thread.id
|
| 25 |
+
|
| 26 |
+
thread_id = thread_store[user_id]
|
| 27 |
+
|
| 28 |
+
# Step 1: Add message to thread
|
| 29 |
+
openai.beta.threads.messages.create(
|
| 30 |
+
thread_id=thread_id,
|
| 31 |
+
role="user",
|
| 32 |
+
content=user_input
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
# Step 2: Run the assistant
|
| 36 |
+
run = openai.beta.threads.runs.create(
|
| 37 |
+
thread_id=thread_id,
|
| 38 |
+
assistant_id=ASSISTANT_ID
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
# Step 3: Wait for completion (polling)
|
| 42 |
+
import time
|
| 43 |
+
while True:
|
| 44 |
+
run_status = openai.beta.threads.runs.retrieve(thread_id=thread_id, run_id=run.id)
|
| 45 |
+
if run_status.status == "completed":
|
| 46 |
+
break
|
| 47 |
+
elif run_status.status == "failed":
|
| 48 |
+
return jsonify({"error": "Run failed"}), 500
|
| 49 |
+
time.sleep(1)
|
| 50 |
+
|
| 51 |
+
# Step 4: Fetch response
|
| 52 |
+
messages = openai.beta.threads.messages.list(thread_id=thread_id)
|
| 53 |
+
response_text = messages.data[0].content[0].text.value
|
| 54 |
+
|
| 55 |
+
return jsonify({"response": response_text})
|
| 56 |
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
if __name__ == "__main__":
|
| 59 |
+
app.run(debug=True)
|
| 60 |
|
|
|