Spaces:
No application file
No application file
from flask import Flask, render_template_string, request | |
import openai | |
# OpenAI API Key | |
openai.api_key = "YOUR_API_KEY" | |
app = Flask(__name__) | |
HTML_TEMPLATE = """ | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>AI Task Automation</title> | |
</head> | |
<body> | |
<h1>AI-Powered Assistant</h1> | |
<form method="POST"> | |
<textarea name="prompt" placeholder="Ask me anything..." rows="4" cols="50"></textarea><br> | |
<button type="submit">Submit</button> | |
</form> | |
{% if response %} | |
<h3>Response:</h3> | |
<p>{{response}}</p> | |
{% endif %} | |
</body> | |
</html> | |
""" | |
def ai_assistant(): | |
response = None | |
if request.method == "POST": | |
prompt = request.form["prompt"] | |
ai_response = openai.Completion.create( | |
engine="text-davinci-003", | |
prompt=prompt, | |
max_tokens=100 | |
) | |
response = ai_response["choices"][0]["text"].strip() | |
return render_template_string(HTML_TEMPLATE, response=response) | |
if __name__ == "__main__": | |
app.run(debug=True) | |