Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
CHANGED
@@ -6,6 +6,7 @@ import cv2
|
|
6 |
import torch
|
7 |
import torch.nn as nn
|
8 |
from PIL import Image
|
|
|
9 |
import torchvision
|
10 |
from torchvision import transforms
|
11 |
from timm.data.constants import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD
|
@@ -105,18 +106,24 @@ def run_chatbot(input, max_tokens,temperature,frequency_penalty,presence_penalty
|
|
105 |
return conversation,[history,conversation]
|
106 |
|
107 |
def run_chatbot_with_img(input_img,max_tokens,temperature,frequency_penalty,presence_penalty,gr_state=[]):
|
|
|
|
|
|
|
|
|
|
|
108 |
history, conversation = gr_state[0],gr_state[1]
|
109 |
|
110 |
# TODO: save img and show in conversation
|
111 |
# save_img(input_img)
|
112 |
|
113 |
|
114 |
-
img_cls = recognize_image(
|
115 |
# conversation = conversation+ [(f'<img src="/file={input_img.name}" style="display: inline-block;">', "")]
|
116 |
input = 'I have given you a photo about '+ img_cls + ', and tell me its definition.'
|
117 |
output = chat_with_GPT(input,history,max_tokens,temperature,frequency_penalty,presence_penalty)
|
118 |
|
119 |
-
input_mask = '
|
|
|
120 |
# conversation save chatbox content
|
121 |
conversation.append((input_mask,output))
|
122 |
# history for GPT
|
@@ -125,7 +132,7 @@ def run_chatbot_with_img(input_img,max_tokens,temperature,frequency_penalty,pres
|
|
125 |
# chatbox gr_state
|
126 |
return conversation , [history,conversation]
|
127 |
|
128 |
-
def save_img(image):
|
129 |
|
130 |
filename = next(tempfile._get_candidate_names()) + '.png'
|
131 |
print(filename)
|
@@ -151,6 +158,8 @@ def reset():
|
|
151 |
eval_transforms = build_transforms(224)
|
152 |
|
153 |
|
|
|
|
|
154 |
|
155 |
with gr.Blocks() as demo:
|
156 |
gr.HTML("""
|
@@ -177,7 +186,7 @@ with gr.Blocks() as demo:
|
|
177 |
image_btn = gr.UploadButton("Upload Image", file_types=["image"])
|
178 |
with gr.Column(scale=0.3, min_width=400):
|
179 |
max_tokens = gr.Number(
|
180 |
-
|
181 |
temperature = gr.Slider(
|
182 |
minimum=0.0, maximum=1.0, value=0.0, interactive=True, label="Diversity of generated text")
|
183 |
frequency_penalty = gr.Slider(minimum=-2.0, maximum=2.0, value=0.5,
|
@@ -196,6 +205,6 @@ with gr.Blocks() as demo:
|
|
196 |
submit_btn.click(lambda: "", None, text_input)
|
197 |
reset_btn.click(fn=reset,inputs=[],outputs=[chatbot,gr_state])
|
198 |
submit_btn_img.click(run_chatbot_with_img, [image,max_tokens,temperature,frequency_penalty,presence_penalty,gr_state], [chatbot,gr_state])
|
199 |
-
|
200 |
|
201 |
demo.launch(debug = True)
|
|
|
6 |
import torch
|
7 |
import torch.nn as nn
|
8 |
from PIL import Image
|
9 |
+
import numpy
|
10 |
import torchvision
|
11 |
from torchvision import transforms
|
12 |
from timm.data.constants import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD
|
|
|
106 |
return conversation,[history,conversation]
|
107 |
|
108 |
def run_chatbot_with_img(input_img,max_tokens,temperature,frequency_penalty,presence_penalty,gr_state=[]):
|
109 |
+
print(type(input_img))
|
110 |
+
|
111 |
+
img_save = Image.open(input_img.name).resize((224,224)).convert('RGB')
|
112 |
+
img_save.save(input_img.name)
|
113 |
+
img4cls = numpy.array(img_save)
|
114 |
history, conversation = gr_state[0],gr_state[1]
|
115 |
|
116 |
# TODO: save img and show in conversation
|
117 |
# save_img(input_img)
|
118 |
|
119 |
|
120 |
+
img_cls = recognize_image(img4cls)
|
121 |
# conversation = conversation+ [(f'<img src="/file={input_img.name}" style="display: inline-block;">', "")]
|
122 |
input = 'I have given you a photo about '+ img_cls + ', and tell me its definition.'
|
123 |
output = chat_with_GPT(input,history,max_tokens,temperature,frequency_penalty,presence_penalty)
|
124 |
|
125 |
+
input_mask = f'<img src="/file={input_img.name}" style="display: inline-block;">'
|
126 |
+
# input_mask = 'Upload image'
|
127 |
# conversation save chatbox content
|
128 |
conversation.append((input_mask,output))
|
129 |
# history for GPT
|
|
|
132 |
# chatbox gr_state
|
133 |
return conversation , [history,conversation]
|
134 |
|
135 |
+
def save_img(image: Image.Image):
|
136 |
|
137 |
filename = next(tempfile._get_candidate_names()) + '.png'
|
138 |
print(filename)
|
|
|
158 |
eval_transforms = build_transforms(224)
|
159 |
|
160 |
|
161 |
+
import openai
|
162 |
+
import os
|
163 |
|
164 |
with gr.Blocks() as demo:
|
165 |
gr.HTML("""
|
|
|
186 |
image_btn = gr.UploadButton("Upload Image", file_types=["image"])
|
187 |
with gr.Column(scale=0.3, min_width=400):
|
188 |
max_tokens = gr.Number(
|
189 |
+
value=1000, precision=1, interactive=True, label="Maximum length of generated text")
|
190 |
temperature = gr.Slider(
|
191 |
minimum=0.0, maximum=1.0, value=0.0, interactive=True, label="Diversity of generated text")
|
192 |
frequency_penalty = gr.Slider(minimum=-2.0, maximum=2.0, value=0.5,
|
|
|
205 |
submit_btn.click(lambda: "", None, text_input)
|
206 |
reset_btn.click(fn=reset,inputs=[],outputs=[chatbot,gr_state])
|
207 |
submit_btn_img.click(run_chatbot_with_img, [image,max_tokens,temperature,frequency_penalty,presence_penalty,gr_state], [chatbot,gr_state])
|
208 |
+
image_btn.upload(run_chatbot_with_img, [image_btn,max_tokens,temperature,frequency_penalty,presence_penalty,gr_state], [chatbot,gr_state])
|
209 |
|
210 |
demo.launch(debug = True)
|