-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathai-bot-by-hackit
52 lines (42 loc) · 1.41 KB
/
ai-bot-by-hackit
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
import openai
from flask import Flask, request
from twilio.twiml.messaging_response import MessagingResponse
# Flask app configuration
app = Flask(__name__)
# Set your OpenAI API key
openai.api_key = "your-openai-apikey"
def get_chatgpt_response(prompt):
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "user", "content": prompt}
]
)
print(response)
return response.choices[0].message['content']
def generate_dalle_image(prompt):
response = openai.Image.create(
prompt=prompt,
n=2,
size="1024x1024",
)
return response['data'][0]['url']
@app.route("/webhook", methods=["POST"])
def webhook():
# Get the incoming message
incoming_msg = request.values.get("Body", "").lower()
# Get ChatGPT response
chatgpt_response = get_chatgpt_response(incoming_msg)
# Check if the user wants to create AI art using DALL-E
resp = MessagingResponse()
if "create ai art" in incoming_msg:
# Generate DALL-E image based on ChatGPT response
image_url = generate_dalle_image(chatgpt_response)
# Add the image and text response to the message
resp.message(chatgpt_response).media(image_url)
else:
# Add only the text response to the message
resp.message(chatgpt_response)
return str(resp)
if __name__ == "__main__":
app.run(debug=True)