Spaces:
Sleeping
Sleeping
MaziyarPanahi
commited on
Commit
•
1117f0e
1
Parent(s):
10c3e80
Update app.py (#22)
Browse files- Update app.py (5c0dfe46b2e67fa3f0316a7361086b2b171cbe0a)
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 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
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"]},
|