Pavithiran commited on
Commit
c888270
·
verified ·
1 Parent(s): cff7963

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -19
app.py CHANGED
@@ -77,8 +77,9 @@ def respond(
77
  max_tokens,
78
  temperature,
79
  top_p,
80
- image: Image
81
  ):
 
82
  messages = [{"role": "system", "content": system_message}]
83
 
84
  for val in history:
@@ -87,43 +88,38 @@ def respond(
87
  if val[1]:
88
  messages.append({"role": "assistant", "content": val[1]})
89
 
 
90
  messages.append({"role": "user", "content": message})
91
 
92
- # Convert image to the format expected by the model
93
  image_bytes = io.BytesIO()
94
  image.save(image_bytes, format='PNG')
95
  image_bytes.seek(0)
96
 
97
- # Use a different method for image inputs if chat_completion does not support it
98
- response = ""
99
-
100
- # Modify here to use the correct API for image + text queries (check model docs)
101
- response_data = client.text2image(images=image_bytes, text=message) # example method; adjust according to actual API
102
 
103
- # Process the response (if any) from the image model
104
- for result in response_data:
105
- response += result['text'] # or adjust based on response format
 
 
106
 
107
  return response
108
 
 
109
  demo = gr.ChatInterface(
110
  respond,
111
  additional_inputs=[
112
  gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
113
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
114
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
115
- gr.Slider(
116
- minimum=0.1,
117
- maximum=1.0,
118
- value=0.95,
119
- step=0.05,
120
- label="Top-p (nucleus sampling)",
121
- ),
122
  gr.Image(type="pil", label="Upload an Image"), # Image input for vision tasks
123
  ],
124
  )
125
 
126
  if __name__ == "__main__":
127
- demo.launch()
128
-
129
 
 
77
  max_tokens,
78
  temperature,
79
  top_p,
80
+ image: Image, # Add image input to the function
81
  ):
82
+ # Prepare the system message and history for the conversation
83
  messages = [{"role": "system", "content": system_message}]
84
 
85
  for val in history:
 
88
  if val[1]:
89
  messages.append({"role": "assistant", "content": val[1]})
90
 
91
+ # Add the current user message
92
  messages.append({"role": "user", "content": message})
93
 
94
+ # Convert the PIL image to a byte stream
95
  image_bytes = io.BytesIO()
96
  image.save(image_bytes, format='PNG')
97
  image_bytes.seek(0)
98
 
99
+ # Use InferenceClient to handle the image and text input to the model
100
+ # Use the correct method text_to_image
101
+ response_data = client.text_to_image(images=image_bytes, text=message) # Corrected method name
 
 
102
 
103
+ # Process the response from the model
104
+ response = ""
105
+ if response_data:
106
+ for result in response_data:
107
+ response += result.get('text', '') # Process based on the response format
108
 
109
  return response
110
 
111
+ # Create the Gradio interface with an image input
112
  demo = gr.ChatInterface(
113
  respond,
114
  additional_inputs=[
115
  gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
116
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
117
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
118
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
 
 
 
 
 
 
119
  gr.Image(type="pil", label="Upload an Image"), # Image input for vision tasks
120
  ],
121
  )
122
 
123
  if __name__ == "__main__":
124
+ demo.launch(share=True) # Set share=True to create a public link
 
125