tommy24 commited on
Commit
24eae35
·
1 Parent(s): 982ddf1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -12
app.py CHANGED
@@ -88,7 +88,8 @@ import requests
88
  import openai
89
  import random
90
  import base64
91
- import os
 
92
 
93
  def function(Textbox, Textbox2, Textbox3):
94
  target = os.environ.get("target")
@@ -138,16 +139,17 @@ def function(Textbox, Textbox2, Textbox3):
138
  else:
139
  return data
140
  else:
141
- def send_request(reply):
142
  try:
143
- response = requests.get(f"https://api.placid.app/u/pydav4ibo?quote[text]={reply}")
144
- print(f'Response HTTP Status Code: {response.status_code}')
145
- response.raise_for_status()
146
- image = Image.open(BytesIO(response.content))
147
- image_array = np.array(image)
148
- return image_array
149
- except requests.exceptions.RequestException as e:
150
- print('HTTP Request failed:', str(e))
 
151
 
152
  if Textbox3 == target:
153
  Textbox2 = Textbox2.replace("[", "").replace("]", "").replace("'", "")
@@ -162,14 +164,14 @@ def function(Textbox, Textbox2, Textbox3):
162
  messages.append(
163
  {"role": "user", "content": i}
164
  )
165
- try:
166
  sleep(0.8)
167
  chat = openai.ChatCompletion.create(
168
  model="gpt-3.5-turbo", messages=messages
169
  )
170
  print(messages)
171
  reply = chat.choices[0].message.content
172
- image_array = send_request(reply)
173
  messages.append({"role": "assistant", "content": reply})
174
  return image_array
175
  except Exception as e:
@@ -191,6 +193,7 @@ iface = gr.Interface(fn=function, inputs=inputs, outputs=outputs)
191
  iface.launch()
192
 
193
 
 
194
  # from time import sleep
195
  # import gradio as gr
196
  # import traceback
 
88
  import openai
89
  import random
90
  import base64
91
+ import os
92
+ import urllib.request
93
 
94
  def function(Textbox, Textbox2, Textbox3):
95
  target = os.environ.get("target")
 
139
  else:
140
  return data
141
  else:
142
+ def download_image(url):
143
  try:
144
+ with urllib.request.urlopen(url) as response:
145
+ image_data = response.read()
146
+ image = Image.open(BytesIO(image_data))
147
+ image_array = np.array(image)
148
+ return image_array
149
+ except urllib.error.URLError as e:
150
+ print('URL Error:', str(e))
151
+ except IOError as e:
152
+ print('IO Error:', str(e))
153
 
154
  if Textbox3 == target:
155
  Textbox2 = Textbox2.replace("[", "").replace("]", "").replace("'", "")
 
164
  messages.append(
165
  {"role": "user", "content": i}
166
  )
167
+ try:
168
  sleep(0.8)
169
  chat = openai.ChatCompletion.create(
170
  model="gpt-3.5-turbo", messages=messages
171
  )
172
  print(messages)
173
  reply = chat.choices[0].message.content
174
+ image_array = download_image(reply)
175
  messages.append({"role": "assistant", "content": reply})
176
  return image_array
177
  except Exception as e:
 
193
  iface.launch()
194
 
195
 
196
+
197
  # from time import sleep
198
  # import gradio as gr
199
  # import traceback