Spaces:
Running
on
Zero
Running
on
Zero
MaziyarPanahi
commited on
Commit
•
70f2766
1
Parent(s):
1117f0e
Update app.py (#23)
Browse files- Update app.py (9f6b59fbe7f7757f88fdcc4e5ba99651ce3bab65)
app.py
CHANGED
@@ -39,36 +39,38 @@ def bot_streaming(message, history):
|
|
39 |
if type(hist[0])==tuple:
|
40 |
image = hist[0][0]
|
41 |
|
|
|
42 |
try:
|
43 |
-
|
44 |
-
|
45 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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"]},
|
|
|
39 |
if type(hist[0])==tuple:
|
40 |
image = hist[0][0]
|
41 |
|
42 |
+
|
43 |
try:
|
44 |
+
if image is None:
|
45 |
+
# Handle the case where image is None
|
46 |
+
gr.Error("You need to upload an image for LLaVA to work.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
except NameError:
|
48 |
+
# Handle the case where 'image' is not defined at all
|
49 |
+
gr.Error("You need to upload an image for LLaVA to work.")
|
50 |
+
|
51 |
+
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"
|
52 |
+
print(f"prompt: {prompt}")
|
53 |
+
image = Image.open(image)
|
54 |
+
inputs = processor(prompt, image, return_tensors='pt').to(0, torch.float16)
|
55 |
+
|
56 |
+
streamer = TextIteratorStreamer(processor, **{"skip_special_tokens": True})
|
57 |
+
generation_kwargs = dict(inputs, streamer=streamer, max_new_tokens=1024)
|
58 |
+
generated_text = ""
|
59 |
+
|
60 |
+
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
61 |
+
thread.start()
|
62 |
+
|
63 |
+
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"
|
64 |
+
print(f"text_prompt: {text_prompt}")
|
65 |
+
|
66 |
+
buffer = ""
|
67 |
+
for new_text in streamer:
|
68 |
+
|
69 |
+
buffer += new_text
|
70 |
+
|
71 |
+
generated_text_without_prompt = buffer[len(text_prompt):]
|
72 |
+
time.sleep(0.08)
|
73 |
+
yield generated_text_without_prompt
|
74 |
|
75 |
|
76 |
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"]},
|