Spaces:
Sleeping
Sleeping
| from flask import Flask, request, render_template | |
| import openai | |
| import time | |
| import random | |
| # Set up your OpenAI API credentials | |
| openai.api_key = 'YOUR_API_KEY' | |
| # Initialize the Flask application | |
| app = Flask(__name__) | |
| # Helper function for the TypeShuffle animation | |
| def type_shuffle(text, delay=0.03): | |
| result = "" | |
| for char in text: | |
| random_chars = random.choice(['!', '@', '#', '$', '%', '^', '&', '*', '-', '=', '+', '<', '>']) | |
| result += random_chars | |
| time.sleep(delay) | |
| time.sleep(1) | |
| return result + text | |
| # Generate a response from the ChatGPT model | |
| def generate_response(prompt): | |
| response = openai.ChatCompletion.create( | |
| model="gpt-3.5-turbo", | |
| messages=[ | |
| { | |
| "role": "system", | |
| "content": "You are a helpful assistant." | |
| }, | |
| { | |
| "role": "user", | |
| "content": prompt | |
| } | |
| ], | |
| ) | |
| return response['choices'][0]['message']['content'] | |
| def index(): | |
| if request.method == 'POST': | |
| user_input = request.form['user_input'] | |
| # Generate response | |
| response = generate_response(user_input) | |
| # Get the response with TypeShuffle animation | |
| animated_response = type_shuffle(response) | |
| return render_template('index.html', user_input=user_input, animated_response=animated_response) | |
| return render_template('index.html') | |
| if __name__ == "__main__": | |
| app.run(port=5000, debug=True) | |