Spaces:
No application file
No application file
File size: 1,057 Bytes
ebd792c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
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>
"""
@app.route("/", methods=["GET", "POST"])
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)
|