leilaaaaa commited on
Commit
0a459f9
·
verified ·
1 Parent(s): 7821b11

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -46
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 chat_with_llava(image, question):
16
- try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  # Convert image to base64
18
  image_b64 = image_to_base64(image)
 
19
 
20
- # Crafting a prompt to instruct the model to respond as a doctor
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
- # URL for Ollama chat API endpoint
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
- # Prepare payload for the API request
38
- payload = {
39
- "model": "rohithbojja/llava-med-v1.6",
40
- "messages": [
41
- {
42
- "role": "user",
43
- "content": doctor_prompt,
44
- "image": image_b64
45
- }
46
- ]
47
- }
48
 
49
- # Make POST request to Ollama chat API
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
- iface = gr.Interface(
61
- fn=chat_with_llava,
62
  inputs=[
63
- gr.inputs.Image(type="pil", label="Upload Medical Image"),
64
- gr.inputs.Textbox(lines=2, label="Ask a medical question about the image")
 
65
  ],
66
  outputs=gr.outputs.Textbox(label="Response", placeholder="Model response will appear here..."),
67
- title="LLAVA Model - Medical Image and Question",
68
- description="Upload a medical image and ask a specific question about the image for a medical description."
69
  )
70
 
71
  # Launch the Gradio interface
72
  if __name__ == "__main__":
73
- iface.launch()
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()