sagar007 commited on
Commit
80e2071
·
verified ·
1 Parent(s): d6b2ea5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -37
app.py CHANGED
@@ -106,7 +106,7 @@ def process_vision_query(image, text_input):
106
  response = vision_processor.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
107
  return response
108
 
109
- # Combined chat function
110
  def combined_chat(message, image, history, system_prompt, temperature, max_new_tokens, top_p, top_k):
111
  if image is not None:
112
  # Process image query
@@ -117,42 +117,41 @@ def combined_chat(message, image, history, system_prompt, temperature, max_new_t
117
  # Process text query
118
  return stream_text_chat(message, history, system_prompt, temperature, max_new_tokens, top_p, top_k), None
119
 
 
 
 
 
 
 
 
120
  # Custom CSS
121
  custom_css = """
122
- body { background-color: #0b0f19; color: #e2e8f0; font-family: 'Arial', sans-serif;}
123
- #custom-header { text-align: center; padding: 20px 0; background-color: #1a202c; margin-bottom: 20px; border-radius: 10px;}
124
- #custom-header h1 { font-size: 2.5rem; margin-bottom: 0.5rem;}
125
- #custom-header h1 .blue { color: #60a5fa;}
126
- #custom-header h1 .pink { color: #f472b6;}
127
- #custom-header h2 { font-size: 1.5rem; color: #94a3b8;}
128
- .gradio-container { max-width: 100% !important;}
129
- #component-0, #component-1, #component-2 { max-width: 100% !important;}
130
- footer { text-align: center; margin-top: 2rem; color: #64748b;}
131
- """
132
-
133
- # Custom HTML for the header
134
- custom_header = """
135
- <div id="custom-header">
136
- <h1><span class="blue">Phi 3.5</span> <span class="pink">Multimodal Assistant</span></h1>
137
- <h2>Text and Vision AI at Your Service</h2>
138
- </div>
139
  """
140
 
141
  # Gradio interface
142
- with gr.Blocks(css=custom_css, theme=gr.themes.Base().set(
143
- body_background_fill="#0b0f19",
144
- body_text_color="#e2e8f0",
145
- button_primary_background_fill="#3b82f6",
146
- button_primary_background_fill_hover="#2563eb",
147
- button_primary_text_color="white",
148
- block_title_text_color="#94a3b8",
149
- block_label_text_color="#94a3b8",
150
- )) as demo:
151
- gr.HTML(custom_header)
152
-
153
- chatbot = gr.Chatbot(height=400)
154
- msg = gr.Textbox(label="Message", placeholder="Type your message here...")
155
- image_input = gr.Image(label="Upload an Image (optional)", type="pil")
 
 
156
 
157
  with gr.Accordion("Advanced Options", open=False):
158
  system_prompt = gr.Textbox(value="You are a helpful assistant", label="System Prompt")
@@ -160,12 +159,10 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Base().set(
160
  max_new_tokens = gr.Slider(minimum=128, maximum=8192, step=1, value=1024, label="Max new tokens")
161
  top_p = gr.Slider(minimum=0.0, maximum=1.0, step=0.1, value=1.0, label="top_p")
162
  top_k = gr.Slider(minimum=1, maximum=20, step=1, value=20, label="top_k")
163
-
164
- submit_btn = gr.Button("Submit", variant="primary")
165
- clear_btn = gr.Button("Clear Chat", variant="secondary")
166
 
167
- submit_btn.click(combined_chat, [msg, image_input, chatbot, system_prompt, temperature, max_new_tokens, top_p, top_k], [chatbot, image_input])
168
- clear_btn.click(lambda: ([], None), None, [chatbot, image_input], queue=False)
 
169
 
170
  gr.HTML("<footer>Powered by Phi 3.5 Multimodal AI</footer>")
171
 
 
106
  response = vision_processor.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
107
  return response
108
 
109
+ # Modified combined chat function
110
  def combined_chat(message, image, history, system_prompt, temperature, max_new_tokens, top_p, top_k):
111
  if image is not None:
112
  # Process image query
 
117
  # Process text query
118
  return stream_text_chat(message, history, system_prompt, temperature, max_new_tokens, top_p, top_k), None
119
 
120
+ # Function to toggle between text and image input
121
+ def toggle_input(choice):
122
+ if choice == "Text":
123
+ return gr.update(visible=True), gr.update(visible=False)
124
+ else:
125
+ return gr.update(visible=False), gr.update(visible=True)
126
+
127
  # Custom CSS
128
  custom_css = """
129
+ body { background-color: #343541; color: #ececf1; font-family: 'Arial', sans-serif; }
130
+ .gradio-container { max-width: 800px !important; margin: auto; }
131
+ #chatbot { height: 400px; overflow-y: auto; }
132
+ #input-container { display: flex; align-items: center; }
133
+ #msg, #image-input { flex-grow: 1; margin-right: 10px; }
134
+ #submit-btn { min-width: 60px; }
135
+ footer { text-align: center; margin-top: 2rem; color: #acacbe; }
 
 
 
 
 
 
 
 
 
 
136
  """
137
 
138
  # Gradio interface
139
+ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
140
+ chatbot = gr.Chatbot(elem_id="chatbot")
141
+
142
+ with gr.Row(elem_id="input-container"):
143
+ input_type = gr.Radio(["Text", "Image"], value="Text", label="Input Type")
144
+ with gr.Column(visible=True) as text_input:
145
+ msg = gr.Textbox(
146
+ show_label=False,
147
+ placeholder="Send a message...",
148
+ elem_id="msg"
149
+ )
150
+ with gr.Column(visible=False) as image_input:
151
+ image = gr.Image(type="pil", elem_id="image-input")
152
+
153
+ submit_btn = gr.Button("Send", elem_id="submit-btn")
154
+ clear_btn = gr.Button("Clear Chat", variant="secondary")
155
 
156
  with gr.Accordion("Advanced Options", open=False):
157
  system_prompt = gr.Textbox(value="You are a helpful assistant", label="System Prompt")
 
159
  max_new_tokens = gr.Slider(minimum=128, maximum=8192, step=1, value=1024, label="Max new tokens")
160
  top_p = gr.Slider(minimum=0.0, maximum=1.0, step=0.1, value=1.0, label="top_p")
161
  top_k = gr.Slider(minimum=1, maximum=20, step=1, value=20, label="top_k")
 
 
 
162
 
163
+ input_type.change(toggle_input, input_type, [text_input, image_input])
164
+ submit_btn.click(combined_chat, [msg, image, chatbot, system_prompt, temperature, max_new_tokens, top_p, top_k], [chatbot, image])
165
+ clear_btn.click(lambda: ([], None), None, [chatbot, image], queue=False)
166
 
167
  gr.HTML("<footer>Powered by Phi 3.5 Multimodal AI</footer>")
168