Spaces:
No application file
No application file
Download main.py from ACADEE/chatgpt_bot: direct link, hf CLI and curl.
- Browser
- Download file 3.04 kB
-
https://huggingface.co/spaces/ACADEE/chatgpt_bot/resolve/main/main.py
- Command line
-
hf download hf://spaces/ACADEE/chatgpt_bot/main.py
-
curl -L -o main.py https://huggingface.co/spaces/ACADEE/chatgpt_bot/resolve/main/main.py
3.04 kB
| import os | |
| from time import sleep | |
| from packaging import version | |
| from flask import Flask, request, jsonify | |
| import openai | |
| from openai import OpenAI | |
| import functions | |
| # Check OpenAI version is correct | |
| required_version = version.parse("1.1.1") | |
| current_version = version.parse(openai.__version__) | |
| OPENAI_API_KEY = os.environ['OPENAI_API_KEY'] | |
| if current_version < required_version: | |
| raise ValueError(f"Error: OpenAI version {openai.__version__}" | |
| " is less than the required version 1.1.1") | |
| else: | |
| print("OpenAI version is compatible.") | |
| # Start Flask app | |
| app = Flask(__name__) | |
| # Init client | |
| client = OpenAI( | |
| api_key=OPENAI_API_KEY) # should use env variable OPENAI_API_KEY in secrets (bottom left corner) | |
| # Create new assistant or load existing | |
| assistant_id = functions.create_assistant(client) | |
| def index(): | |
| return render_template("index.html") | |
| # Start conversation thread | |
| def start_conversation(): | |
| print("Starting a new conversation...") # Debugging line | |
| thread = client.beta.threads.create() | |
| print(f"New thread created with ID: {thread.id}") # Debugging line | |
| return jsonify({"thread_id": thread.id}) | |
| # Generate response | |
| def chat(): | |
| data = request.json | |
| thread_id = data.get('thread_id') | |
| user_input = data.get('message', '') | |
| send_message(msg= user_input); | |
| if not thread_id: | |
| print("Error: Missing thread_id") # Debugging line | |
| return jsonify({"error": "Missing thread_id"}), 400 | |
| print(f"Received message: {user_input} for thread ID: {thread_id}" | |
| ) # Debugging line | |
| # Add the user's message to the thread | |
| client.beta.threads.messages.create(thread_id=thread_id, | |
| role="user", | |
| content=user_input) | |
| # Run the Assistant | |
| run = client.beta.threads.runs.create(thread_id=thread_id, | |
| assistant_id=assistant_id) | |
| # Check if the Run requires action (function call) | |
| while True: | |
| run_status = client.beta.threads.runs.retrieve(thread_id=thread_id, | |
| run_id=run.id) | |
| print(f"Run status: {run_status.status}") | |
| if run_status.status == 'completed': | |
| break | |
| sleep(1) # Wait for a second before checking again | |
| # Retrieve and return the latest message from the assistant | |
| messages = client.beta.threads.messages.list(thread_id=thread_id) | |
| response = messages.data[0].content[0].text.value | |
| # print(f"Assistant response: {response}") # Debugging line | |
| return jsonify({"response": response}) | |
| import requests | |
| import os | |
| def send_message(msg): | |
| webhook_url = os.environ['discord_hook'] | |
| data = { | |
| "username": "Chatbot Acadee", | |
| "content": f"{msg}" | |
| } | |
| response = requests.post(webhook_url, json=data) | |
| return response | |
| # Run server | |
| if __name__ == '__main__': | |
| send_message(msg="Restarting Chatbot Acadee...") # Debugging line | |
| app.run(host='0.0.0.0', port=8080) | |