MaziyarPanahi commited on
Commit
1117f0e
1 Parent(s): 10c3e80

Update app.py (#22)

Browse files

- Update app.py (5c0dfe46b2e67fa3f0316a7361086b2b171cbe0a)

Files changed (1) hide show
  1. app.py +30 -25
app.py CHANGED
@@ -39,31 +39,36 @@ def bot_streaming(message, history):
39
  if type(hist[0])==tuple:
40
  image = hist[0][0]
41
 
42
- if image is None:
43
- gr.Error("You need to upload an image for LLaVA to work.")
44
- prompt=f"<|start_header_id|>user<|end_header_id|>\n\n<image>\n{message['text']}<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n"
45
- print(f"prompt: {prompt}")
46
- image = Image.open(image)
47
- inputs = processor(prompt, image, return_tensors='pt').to(0, torch.float16)
48
-
49
- streamer = TextIteratorStreamer(processor, **{"skip_special_tokens": True})
50
- generation_kwargs = dict(inputs, streamer=streamer, max_new_tokens=1024)
51
- generated_text = ""
52
-
53
- thread = Thread(target=model.generate, kwargs=generation_kwargs)
54
- thread.start()
55
-
56
- text_prompt =f"<|start_header_id|>user<|end_header_id|>\n\n{message['text']}<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n"
57
- print(f"text_prompt: {text_prompt}")
58
-
59
- buffer = ""
60
- for new_text in streamer:
61
-
62
- buffer += new_text
63
-
64
- generated_text_without_prompt = buffer[len(text_prompt):]
65
- time.sleep(0.08)
66
- yield generated_text_without_prompt
 
 
 
 
 
67
 
68
 
69
  demo = gr.ChatInterface(fn=bot_streaming, css=CSS, fill_height=True, title="LLaVA Llama-3-8B", examples=[{"text": "What is on the flower?", "files":["./bee.jpg"]},
 
39
  if type(hist[0])==tuple:
40
  image = hist[0][0]
41
 
42
+ try:
43
+ if image is None:
44
+ # Handle the case where image is None
45
+ gr.Error("You need to upload an image for LLaVA to work.")
46
+ else:
47
+ prompt=f"<|start_header_id|>user<|end_header_id|>\n\n<image>\n{message['text']}<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n"
48
+ print(f"prompt: {prompt}")
49
+ image = Image.open(image)
50
+ inputs = processor(prompt, image, return_tensors='pt').to(0, torch.float16)
51
+
52
+ streamer = TextIteratorStreamer(processor, **{"skip_special_tokens": True})
53
+ generation_kwargs = dict(inputs, streamer=streamer, max_new_tokens=1024)
54
+ generated_text = ""
55
+
56
+ thread = Thread(target=model.generate, kwargs=generation_kwargs)
57
+ thread.start()
58
+
59
+ text_prompt =f"<|start_header_id|>user<|end_header_id|>\n\n{message['text']}<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n"
60
+ print(f"text_prompt: {text_prompt}")
61
+
62
+ buffer = ""
63
+ for new_text in streamer:
64
+
65
+ buffer += new_text
66
+
67
+ generated_text_without_prompt = buffer[len(text_prompt):]
68
+ time.sleep(0.08)
69
+ yield generated_text_without_prompt
70
+ except NameError:
71
+ gr.Error("You need to upload an image for LLaVA to work.")
72
 
73
 
74
  demo = gr.ChatInterface(fn=bot_streaming, css=CSS, fill_height=True, title="LLaVA Llama-3-8B", examples=[{"text": "What is on the flower?", "files":["./bee.jpg"]},