Spaces:
Runtime error
Runtime error
create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
def upload_button_config():
|
| 6 |
+
return gr.update(visible=False)
|
| 7 |
+
|
| 8 |
+
def upload_textbox_config(text_in):
|
| 9 |
+
return gr.update(visible=True)
|
| 10 |
+
|
| 11 |
+
#takes input and generates the Response
|
| 12 |
+
def predict(btn_upload, counter,image_hid, input, history):
|
| 13 |
+
|
| 14 |
+
if counter == 0:
|
| 15 |
+
image_in = Image.open(btn_upload)
|
| 16 |
+
#Resizing the image
|
| 17 |
+
basewidth = 512
|
| 18 |
+
wpercent = (basewidth/float(image_in.size[0]))
|
| 19 |
+
hsize = int((float(image_in.size[1])*float(wpercent)))
|
| 20 |
+
image_in = image_in.resize((basewidth,hsize)) #, Image.Resampling.LANCZOS)
|
| 21 |
+
# Save the image to the file-like object
|
| 22 |
+
#seed = random.randint(0, 1000000)
|
| 23 |
+
img_name = "uploaded_image.png" #f"./edited_image_{seed}.png"
|
| 24 |
+
image_in.save(img_name)
|
| 25 |
+
#add state
|
| 26 |
+
history = history or []
|
| 27 |
+
response = '<img src="/file=' + img_name + '">'
|
| 28 |
+
history.append((input, response))
|
| 29 |
+
counter += 1
|
| 30 |
+
return history, history, img_name, counter, image_in
|
| 31 |
+
|
| 32 |
+
#process the input prompt and image
|
| 33 |
+
#image = Image.open(btn_upload)
|
| 34 |
+
print(f"prompt is :{input}") #Question: Is this photo unusual? Answer:
|
| 35 |
+
input = f"Question: {input} Answer:"
|
| 36 |
+
inputs = processor(image_hid, text=input, return_tensors="pt").to(device, torch.float16)
|
| 37 |
+
|
| 38 |
+
#generte the response
|
| 39 |
+
generated_ids = model.generate(**inputs, max_new_tokens=10)
|
| 40 |
+
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0].strip()
|
| 41 |
+
print(f"generated_text is : {generated_text}")
|
| 42 |
+
|
| 43 |
+
#add state
|
| 44 |
+
history = history or []
|
| 45 |
+
response = generated_text #'<img src="/file=' + img_name + '">'
|
| 46 |
+
history.append((input, response))
|
| 47 |
+
counter += 1
|
| 48 |
+
return history, history, "uploaded_image.png", counter, image_hid
|
| 49 |
+
|
| 50 |
+
#Blocks Layout
|
| 51 |
+
with gr.Blocks() as demo:
|
| 52 |
+
with gr.Column():
|
| 53 |
+
#text_in = gr.Textbox(value='', placeholder="Type your questions here and press enter", elem_id = "input_prompt", visible=False, label='Great! Now you can ask questions to get more information about the image')
|
| 54 |
+
btn_upload = gr.UploadButton("Upload image!", file_types=["image"], file_count="single", elem_id="upload_button")
|
| 55 |
+
chatbot = gr.Chatbot(elem_id = 'chatbot-component', label='Conversational with Images')
|
| 56 |
+
text_in = gr.Textbox(value='', placeholder="Type your questions here and press enter", elem_id = "input_prompt", visible=False, label='Great! Now you can ask questions to get more information about the image')
|
| 57 |
+
state_in = gr.State()
|
| 58 |
+
counter_out = gr.Number(visible=False, value=0, precision=0)
|
| 59 |
+
text_out = gr.Textbox(visible=False) #getting imag name out
|
| 60 |
+
image_hid = gr.Image() #visible=False) #, type='pil')
|
| 61 |
+
|
| 62 |
+
#Using Event Listeners
|
| 63 |
+
btn_upload.upload(predict, [btn_upload, counter_out, image_hid, text_in, state_in], [chatbot, state_in, text_out, counter_out, image_hid])
|
| 64 |
+
btn_upload.upload(fn = upload_textbox_config, inputs=text_in, outputs = text_in)
|
| 65 |
+
|
| 66 |
+
text_in.submit(predict, [btn_upload, counter_out, image_hid, text_in, state_in], [chatbot, state_in, text_out, counter_out, image_hid])
|
| 67 |
+
#text_in.submit(previous, [image_hid], [image_oneup])
|
| 68 |
+
|
| 69 |
+
chatbot.change(fn = upload_button_config, outputs=btn_upload) #, scroll_to_output = True)
|
| 70 |
+
#text_in.submit(None, [], [], _js = "() => document.getElementById('#chatbot-component').scrollTop = document.getElementById('#chatbot-component').scrollHeight")
|
| 71 |
+
|
| 72 |
+
#with gr.Accordion("Release Notes", open=False):
|
| 73 |
+
#gr.Markdown(help_text)
|
| 74 |
+
|
| 75 |
+
demo.queue(concurrency_count=10)
|
| 76 |
+
demo.launch(debug=True) #, width="80%", height=2000)
|