Nithish310 commited on
Commit
2a0c6b6
·
verified ·
1 Parent(s): 143f301

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -46
app.py CHANGED
@@ -14,18 +14,12 @@ import torch
14
  import cv2
15
  from gradio_client import Client, file
16
 
17
- # The image for the bot and human
18
- bot_image = "path_to_bot_image.png"
19
- human_image = "path_to_human_image.png"
20
-
21
  def image_gen(prompt):
22
  client = Client("KingNish/Image-Gen-Pro")
23
- return client.predict("Image Generation",None, prompt, api_name="/image_gen_pro")
24
 
25
  model_id = "llava-hf/llava-interleave-qwen-0.5b-hf"
26
-
27
  processor = LlavaProcessor.from_pretrained(model_id)
28
-
29
  model = LlavaForConditionalGeneration.from_pretrained(model_id)
30
  model.to("cpu")
31
 
@@ -34,12 +28,11 @@ def llava(message, history):
34
  image = message["files"][0]
35
  else:
36
  for hist in history:
37
- if type(hist[0])==tuple:
38
  image = hist[0][0]
39
 
40
  txt = message["text"]
41
 
42
- gr.Info("Analyzing image")
43
  image = Image.open(image).convert("RGB")
44
  prompt = f"user <image>\n{txt}assistant"
45
 
@@ -93,7 +86,6 @@ def respond(message, history):
93
  func_caller = []
94
 
95
  user_prompt = message
96
- # Handle image processing
97
  if message["files"]:
98
  inputs = llava(message, history)
99
  streamer = TextIteratorStreamer(processor, skip_prompt=True, **{"skip_special_tokens": True})
@@ -161,52 +153,59 @@ def respond(message, history):
161
  yield gr.Image(image[1])
162
  except:
163
  client_sd3 = InferenceClient("stabilityai/stable-diffusion-3-medium-diffusers")
164
- seed = random.randint(0,999999)
165
  image = client_sd3.text_to_image(query, negative_prompt=f"{seed}")
166
  yield gr.Image(image)
167
  elif json_data["name"] == "image_qna":
168
- inputs = llava(message, history)
 
 
 
 
 
 
 
 
 
 
 
 
169
  streamer = TextIteratorStreamer(processor, skip_prompt=True, **{"skip_special_tokens": True})
170
  generation_kwargs = dict(inputs, streamer=streamer, max_new_tokens=1024)
171
 
172
  thread = Thread(target=model.generate, kwargs=generation_kwargs)
173
  thread.start()
174
-
175
  buffer = ""
176
  for new_text in streamer:
177
  buffer += new_text
178
  yield buffer
179
- except Exception as ex:
180
- gr.Error(str(ex))
181
-
182
- # Gradio Blocks interface with a custom layout
183
- with gr.Blocks(css="#chatbot .chat-message.user { border-bottom: none; margin-bottom: 2px; }") as demo:
184
- gr.Markdown("# OpenChat Mini 🚀")
185
- chatbot = gr.Chatbot(label="Nithish OpenChat").style(container=False).style(height=700)
186
- with gr.Row():
187
- with gr.Column(scale=0.95):
188
- with gr.Row():
189
- txt = gr.Textbox(show_label=False, placeholder="Type your message here...").style(container=False)
190
- with gr.Row():
191
- file_btn = gr.UploadButton("📁", file_types=["image", "video", "document"])
192
- with gr.Row():
193
- submit_btn = gr.Button("Send", size="small", variant="primary")
194
- with gr.Column(scale=0.05, align="center"):
195
- gr.Image.update(human_image, human_image)
196
-
197
  with gr.Row():
198
- with gr.Column(scale=0.95):
199
- with gr.Row():
200
- file_btn2 = gr.UploadButton("📁", file_types=["image", "video", "document"]).style(container=False)
201
- with gr.Row():
202
- bot_img = gr.Image(bot_image, bot_image).style(container=False)
203
- with gr.Row():
204
- submit_btn2 = gr.Button("Send", size="small", variant="primary")
205
-
206
- txt.submit(respond, [txt, chatbot], [txt, chatbot], scroll_to_output=True)
207
- file_btn.upload(respond, [txt, chatbot], [txt, chatbot], scroll_to_output=True)
208
- submit_btn.click(respond, [txt, chatbot], [txt, chatbot], scroll_to_output=True)
209
- file_btn2.upload(respond, [txt, chatbot], [txt, chatbot], scroll_to_output=True)
210
- submit_btn2.click(respond, [txt, chatbot], [txt, chatbot], scroll_to_output=True)
211
-
212
- demo.launch(debug=True)
 
14
  import cv2
15
  from gradio_client import Client, file
16
 
 
 
 
 
17
  def image_gen(prompt):
18
  client = Client("KingNish/Image-Gen-Pro")
19
+ return client.predict("Image Generation", None, prompt, api_name="/image_gen_pro")
20
 
21
  model_id = "llava-hf/llava-interleave-qwen-0.5b-hf"
 
22
  processor = LlavaProcessor.from_pretrained(model_id)
 
23
  model = LlavaForConditionalGeneration.from_pretrained(model_id)
24
  model.to("cpu")
25
 
 
28
  image = message["files"][0]
29
  else:
30
  for hist in history:
31
+ if type(hist[0]) == tuple:
32
  image = hist[0][0]
33
 
34
  txt = message["text"]
35
 
 
36
  image = Image.open(image).convert("RGB")
37
  prompt = f"user <image>\n{txt}assistant"
38
 
 
86
  func_caller = []
87
 
88
  user_prompt = message
 
89
  if message["files"]:
90
  inputs = llava(message, history)
91
  streamer = TextIteratorStreamer(processor, skip_prompt=True, **{"skip_special_tokens": True})
 
153
  yield gr.Image(image[1])
154
  except:
155
  client_sd3 = InferenceClient("stabilityai/stable-diffusion-3-medium-diffusers")
156
+ seed = random.randint(0, 999999)
157
  image = client_sd3.text_to_image(query, negative_prompt=f"{seed}")
158
  yield gr.Image(image)
159
  elif json_data["name"] == "image_qna":
160
+ if "files" in message:
161
+ image = message["files"][0]
162
+ else:
163
+ for hist in history:
164
+ if type(hist[0]) == tuple:
165
+ image = hist[0][0]
166
+
167
+ txt = json_data["arguments"]["query"]
168
+
169
+ image = Image.open(image).convert("RGB")
170
+ prompt = f"user <image>\n{txt}assistant"
171
+
172
+ inputs = processor(prompt, image, return_tensors="pt")
173
  streamer = TextIteratorStreamer(processor, skip_prompt=True, **{"skip_special_tokens": True})
174
  generation_kwargs = dict(inputs, streamer=streamer, max_new_tokens=1024)
175
 
176
  thread = Thread(target=model.generate, kwargs=generation_kwargs)
177
  thread.start()
178
+
179
  buffer = ""
180
  for new_text in streamer:
181
  buffer += new_text
182
  yield buffer
183
+ except:
184
+ messages = ""
185
+ for msg in history:
186
+ messages += f"\nuser\n{str(msg[0])}"
187
+ messages += f"\nassistant\n{str(msg[1])}"
188
+ messages += f"\nuser\n{str(message)}"
189
+ stream = client_llama.text_generation(messages, max_new_tokens=2000, do_sample=True, stream=True, details=True, return_full_text=False)
190
+ output = ""
191
+ for response in stream:
192
+ if not response.token.text == "":
193
+ output += response.token.text
194
+ yield output
195
+
196
+ with gr.Blocks() as demo:
197
+ history = gr.Chatbot(label="ChatGPT Style Chatbot", height=500).style(container=False)
198
+ input_message = gr.Textbox(label="Message", placeholder="Type your message here...", show_label=False, container=False)
199
+ input_files = gr.File(label="Attach an image", file_types=["image"], container=False)
200
+
201
  with gr.Row():
202
+ submit_button = gr.Button("Submit")
203
+ clear_button = gr.Button("Clear")
204
+
205
+ def send_message(message, files):
206
+ return respond(message, history)
207
+
208
+ submit_button.click(send_message, inputs=[input_message, input_files], outputs=history)
209
+ clear_button.click(lambda: [], None, history)
210
+
211
+ demo.launch()