Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,10 @@ import gradio as gr
|
|
2 |
from PIL import Image
|
3 |
import io
|
4 |
import base64
|
|
|
|
|
|
|
|
|
5 |
|
6 |
# Function to encode image as base64
|
7 |
def image_to_base64(image):
|
@@ -11,7 +15,15 @@ def image_to_base64(image):
|
|
11 |
return img_str
|
12 |
|
13 |
# Function to interact with LLAVA model
|
14 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
messages = [{"role": "system", "content": system_message}]
|
16 |
|
17 |
for val in history:
|
@@ -20,34 +32,43 @@ def chat_with_llava(message, history, system_message, max_tokens, temperature, t
|
|
20 |
if val[1]:
|
21 |
messages.append({"role": "assistant", "content": val[1]})
|
22 |
|
|
|
|
|
23 |
if image:
|
24 |
# Convert image to base64
|
25 |
image_b64 = image_to_base64(image)
|
26 |
messages.append({"role": "user", "content": "Image uploaded", "image": image_b64})
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
# Simulate a response (replace with your logic)
|
31 |
response = ""
|
32 |
-
for
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
-
|
|
|
36 |
|
37 |
# Create a Gradio interface
|
38 |
demo = gr.Interface(
|
39 |
-
fn=
|
40 |
inputs=[
|
41 |
-
gr.Textbox(label="Message"),
|
42 |
-
gr.Image(label="Upload Medical Image")
|
43 |
-
gr.Textbox(label="System message", default="You are a friendly Chatbot."),
|
44 |
-
gr.Slider(minimum=1, maximum=2048, default=512, step=1, label="Max new tokens"),
|
45 |
-
gr.Slider(minimum=0.1, maximum=4.0, default=0.7, step=0.1, label="Temperature"),
|
46 |
-
gr.Slider(minimum=0.1, maximum=1.0, default=0.95, step=0.05, label="Top-p (nucleus sampling)")
|
47 |
],
|
48 |
outputs=gr.outputs.Textbox(label="Response", placeholder="Model response will appear here..."),
|
49 |
title="LLAVA Model - Medical Image and Question",
|
50 |
-
description="Upload a medical image and ask a specific question about the image for a medical description."
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
)
|
52 |
|
53 |
# Launch the Gradio interface
|
|
|
2 |
from PIL import Image
|
3 |
import io
|
4 |
import base64
|
5 |
+
from huggingface_hub import InferenceClient
|
6 |
+
|
7 |
+
# Initialize the Hugging Face Inference Client
|
8 |
+
client = InferenceClient("microsoft/llava-med-7b-delta")
|
9 |
|
10 |
# Function to encode image as base64
|
11 |
def image_to_base64(image):
|
|
|
15 |
return img_str
|
16 |
|
17 |
# Function to interact with LLAVA model
|
18 |
+
def respond(
|
19 |
+
message,
|
20 |
+
history: list[tuple[str, str]],
|
21 |
+
system_message,
|
22 |
+
max_tokens,
|
23 |
+
temperature,
|
24 |
+
top_p,
|
25 |
+
image=None
|
26 |
+
):
|
27 |
messages = [{"role": "system", "content": system_message}]
|
28 |
|
29 |
for val in history:
|
|
|
32 |
if val[1]:
|
33 |
messages.append({"role": "assistant", "content": val[1]})
|
34 |
|
35 |
+
messages.append({"role": "user", "content": message})
|
36 |
+
|
37 |
if image:
|
38 |
# Convert image to base64
|
39 |
image_b64 = image_to_base64(image)
|
40 |
messages.append({"role": "user", "content": "Image uploaded", "image": image_b64})
|
41 |
|
42 |
+
# Call Hugging Face model for response
|
|
|
|
|
43 |
response = ""
|
44 |
+
for message in client.chat_completion(
|
45 |
+
messages,
|
46 |
+
max_tokens=max_tokens,
|
47 |
+
stream=True,
|
48 |
+
temperature=temperature,
|
49 |
+
top_p=top_p,
|
50 |
+
):
|
51 |
+
token = message.choices[0].delta.content
|
52 |
|
53 |
+
response += token
|
54 |
+
yield response
|
55 |
|
56 |
# Create a Gradio interface
|
57 |
demo = gr.Interface(
|
58 |
+
fn=respond,
|
59 |
inputs=[
|
60 |
+
gr.inputs.Textbox(label="Message"),
|
61 |
+
gr.inputs.Image(label="Upload Medical Image", type="pil")
|
|
|
|
|
|
|
|
|
62 |
],
|
63 |
outputs=gr.outputs.Textbox(label="Response", placeholder="Model response will appear here..."),
|
64 |
title="LLAVA Model - Medical Image and Question",
|
65 |
+
description="Upload a medical image and ask a specific question about the image for a medical description.",
|
66 |
+
additional_inputs=[
|
67 |
+
gr.Textbox(label="System message", default="You are a friendly Chatbot."),
|
68 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
69 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
70 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")
|
71 |
+
]
|
72 |
)
|
73 |
|
74 |
# Launch the Gradio interface
|