Update app.py
Browse files
app.py
CHANGED
@@ -2,7 +2,6 @@ import gradio as gr
|
|
2 |
from PIL import Image
|
3 |
import io
|
4 |
import base64
|
5 |
-
import requests # For making API requests
|
6 |
|
7 |
# Function to encode image as base64
|
8 |
def image_to_base64(image):
|
@@ -12,63 +11,51 @@ def image_to_base64(image):
|
|
12 |
return img_str
|
13 |
|
14 |
# Function to interact with LLAVA model
|
15 |
-
def
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
# Convert image to base64
|
18 |
image_b64 = image_to_base64(image)
|
|
|
19 |
|
20 |
-
|
21 |
-
doctor_prompt = (
|
22 |
-
"You are a highly experienced and knowledgeable medical doctor. "
|
23 |
-
"Please analyze the provided medical image and give a detailed medical explanation in response to the following question.\n\n"
|
24 |
-
f"Question: {question}\n"
|
25 |
-
"Please include relevant medical terminology and explanations in your response."
|
26 |
-
)
|
27 |
|
28 |
-
|
29 |
-
api_url = "https://api.ollama.com/chat"
|
30 |
-
|
31 |
-
# Prepare headers with API key (replace 'YOUR_API_KEY' with your actual API key)
|
32 |
-
headers = {
|
33 |
-
"Authorization": "Bearer YOUR_API_KEY",
|
34 |
-
"Content-Type": "application/json"
|
35 |
-
}
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
"messages": [
|
41 |
-
{
|
42 |
-
"role": "user",
|
43 |
-
"content": doctor_prompt,
|
44 |
-
"image": image_b64
|
45 |
-
}
|
46 |
-
]
|
47 |
-
}
|
48 |
|
49 |
-
|
50 |
-
response = requests.post(api_url, json=payload, headers=headers)
|
51 |
-
response_data = response.json()
|
52 |
-
|
53 |
-
# Extract and return model response
|
54 |
-
return response_data['message']['content']
|
55 |
-
|
56 |
-
except Exception as e:
|
57 |
-
return f"Error occurred: {str(e)}"
|
58 |
|
59 |
# Create a Gradio interface
|
60 |
-
|
61 |
-
fn=
|
62 |
inputs=[
|
63 |
-
gr.inputs.
|
64 |
-
gr.inputs.
|
|
|
65 |
],
|
66 |
outputs=gr.outputs.Textbox(label="Response", placeholder="Model response will appear here..."),
|
67 |
-
title="
|
68 |
-
description="
|
69 |
)
|
70 |
|
71 |
# Launch the Gradio interface
|
72 |
if __name__ == "__main__":
|
73 |
-
|
74 |
-
|
|
|
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 |
return img_str
|
12 |
|
13 |
# Function to interact with LLAVA model
|
14 |
+
def respond(
|
15 |
+
message,
|
16 |
+
history: list[tuple[str, str]],
|
17 |
+
system_message,
|
18 |
+
max_tokens,
|
19 |
+
temperature,
|
20 |
+
top_p,
|
21 |
+
image=None
|
22 |
+
):
|
23 |
+
messages = [{"role": "system", "content": system_message}]
|
24 |
+
|
25 |
+
for val in history:
|
26 |
+
if val[0]:
|
27 |
+
messages.append({"role": "user", "content": val[0]})
|
28 |
+
if val[1]:
|
29 |
+
messages.append({"role": "assistant", "content": val[1]})
|
30 |
+
|
31 |
+
if image:
|
32 |
# Convert image to base64
|
33 |
image_b64 = image_to_base64(image)
|
34 |
+
messages.append({"role": "user", "content": "Image uploaded", "image": image_b64})
|
35 |
|
36 |
+
messages.append({"role": "user", "content": message})
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
+
response = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
+
# Simulate a response (replace with your logic)
|
41 |
+
for i in range(3): # Simulate 3 responses
|
42 |
+
response += f"Response {i+1}\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
+
yield response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
# Create a Gradio interface
|
47 |
+
demo = gr.Interface(
|
48 |
+
fn=respond,
|
49 |
inputs=[
|
50 |
+
gr.inputs.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
51 |
+
gr.inputs.Image(type="pil", label="Upload Image"),
|
52 |
+
gr.inputs.Textbox(lines=2, label="Message")
|
53 |
],
|
54 |
outputs=gr.outputs.Textbox(label="Response", placeholder="Model response will appear here..."),
|
55 |
+
title="Chat Interface Demo",
|
56 |
+
description="Simulate a chat interface with customizable parameters."
|
57 |
)
|
58 |
|
59 |
# Launch the Gradio interface
|
60 |
if __name__ == "__main__":
|
61 |
+
demo.launch()
|
|