-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
65 lines (55 loc) · 1.65 KB
/
app.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from flask import Flask, render_template, request
from openai import OpenAI
import os
import time
app = Flask(__name__)
client = OpenAI()
def askGPT(question):
completion = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system",
"content": "You are TARS from interstellar. Be very sarcastic."
},
{
"role": "user",
"content": question
}
]
)
return completion
def createAudio(text):
os.remove("static/output.mp3")
response = client.audio.speech.create(
model="tts-1",
voice="onyx",
input=text,
)
response.stream_to_file("static/output.mp3")
def createImage(prompt):
response = client.images.generate(
model="dall-e-3",
prompt=prompt,
size="1024x1024",
quality="standard",
n=1,
)
return response.data[0].url
@app.route('/', methods=['GET', 'POST'])
def index():
response = ''
if request.method == 'POST':
user_input = request.form['user_input']
try:
completion = askGPT(user_input)
response = completion.choices[0].message.content.strip()
createAudio(response)
imageURL = createImage(response)
except Exception as e:
response = f"Fehler bei der Anfrage: {e}"
return render_template('index.html', response=response, imageURL=imageURL, time=time.time())
@app.route('/home', methods=['GET', 'POST'])
def home():
return render_template('index.html', time=time.time())
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)