-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
150 lines (123 loc) · 4.63 KB
/
main.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import openai
from openai import OpenAI
import logging
from dotenv import load_dotenv
import os
import json
import requests
from PIL import Image
import numpy as np
import random
import gradio as gr
import time
# Setup logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
# Load environment variables from .env file
load_dotenv()
openai_api_key = os.getenv("OPENAI_API_KEY")
if not openai_api_key:
logging.error("OpenAI API key not found in .env file.")
exit(1)
# Initialize OpenAI client
client = OpenAI(api_key=openai_api_key)
# Function to generate image using DALL-E
def generate_dalle_image(prompt):
response = client.images.generate(
model="dall-e-3",
prompt=prompt,
size="1792x1024",
quality="hd",
n=1,
)
return response.data[0].url
# Define tools for image generation
tools = [
{
"type": "function",
"function": {
"name": "generate_dalle_image",
"description": "Generates an image based on a user prompt using DALL-E.",
"parameters": {
"type": "object",
"properties": {
"prompt": {
"type": "string",
"description": "A text prompt to generate an image from."
},
},
"required": ["prompt"]
},
}
}
]
# Global variable to store messages
messages = []
# Function to get chatbot response
def bot_response(user_prompt, max_retries=3):
global messages
messages.append({"role": "user", "content": user_prompt})
attempt = 0
while attempt < max_retries:
try:
completion = client.chat.completions.create(
model="gpt-4-1106-preview",
messages=messages,
tools=tools,
tool_choice="auto"
)
response = completion.choices[0].message
print(response)
tool_call_made = False
if response.tool_calls:
for tool_call in response.tool_calls:
if tool_call.function.name == "generate_dalle_image":
print(response)
tool_call_made = True
try:
prompt = json.loads(tool_call.function.arguments)["prompt"]
except json.JSONDecodeError as e:
logging.error(f"JSON decoding error: {e}")
logging.error(f"Faulty JSON string: {tool_call.function.arguments}")
return "An error occurred while processing the response."
image_url = generate_dalle_image(prompt)
image = Image.open(requests.get(image_url, stream=True).raw)
filename = image_url.split("/")[-1].split("?")[0] + str(np.random.randint(1000)) + ".png"
image.save(filename)
image.show()
messages.append({"role": "assistant", "content": f"Image URL: {image_url}"})
return f"Image URL: {image_url}"
if not tool_call_made:
messages.append({"role": "assistant", "content": response.content})
if len(messages) > 12:
messages = messages[-12:]
return response.content
except Exception as e:
logging.error(f"An error occurred in bot_response: {e}")
if 'content_policy_violation' in str(e):
logging.info("Retrying due to content policy violation...")
attempt += 1
time.sleep(1)
else:
return "An error occurred while generating the response."
return "Failed to generate response after retries."
def main():
while True:
user_input = input("Enter your prompt (or 'exit' to quit): ")
if user_input.lower() == 'exit':
break
response = bot_response(user_input)
print(f"Response: {response}")
with gr.Blocks() as demo:
chatbot = gr.Chatbot()
msg = gr.Textbox()
clear = gr.ClearButton([msg, chatbot])
def respond(message, chat_history):
# Use your existing bot_response function
bot_message = bot_response(message)
# Append to chat history
chat_history.append((message, bot_message))
time.sleep(2) # Optional delay for more natural conversation flow
return "", chat_history
msg.submit(respond, [msg, chatbot], [msg, chatbot])
if __name__ == "__main__":
demo.launch()