Navyabhat commited on
Commit
b4dbe5e
·
verified ·
1 Parent(s): 65f2058

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1 -152
app.py CHANGED
@@ -11,7 +11,6 @@ multimodal_phi2 = MultiModalPhi2(
11
  device="cpu",
12
  )
13
 
14
-
15
  def add_content(chatbot, input_data, input_type) -> gr.Chatbot:
16
  textflag, imageflag, audioflag = False, False, False
17
  if input_type == "text":
@@ -88,155 +87,5 @@ with gr.Blocks() as demo:
88
  # chatbot.like(print_like_dislike, None, None)
89
 
90
 
91
- submit.click(
92
- add_content,
93
- inputs=[chatbot, prompt, image, audio_upload, audio_mic],
94
- outputs=[chatbot],
95
- ).success(
96
- run,
97
- inputs=[chatbot, prompt, image, audio_upload, audio_mic],
98
- outputs=[chatbot, prompt, image, audio_upload, audio_mic],
99
- )
100
-
101
- clear.click(
102
- clear_data,
103
- outputs=[prompt, image, audio_upload, audio_mic, chatbot],
104
- )
105
-
106
- demo.launch()
107
-
108
-
109
- import gradio as gr
110
- from PIL import Image
111
- from inference.main import MultiModalPhi2
112
- import os
113
-
114
- messages = []
115
-
116
- multimodal_phi2 = MultiModalPhi2(
117
- modelname_or_path="Navyabhat/Llava-Phi2",
118
- temperature=0.2,
119
- max_new_tokens=1024,
120
- device="cpu",
121
- )
122
-
123
-
124
- def add_content(chatbot, text, image, audio_upload, audio_mic) -> gr.Chatbot:
125
- textflag, imageflag, audioflag = False, False, False
126
- if text not in ["", None]:
127
- chatbot.append((text, None))
128
- textflag = True
129
- if image is not None:
130
- chatbot.append(((image,), None))
131
- imageflag = True
132
- if audio_mic is not None:
133
- chatbot.append(((audio_mic,), None))
134
- audioflag = True
135
- else:
136
- if audio_upload is not None:
137
- chatbot.append(((audio_upload,), None))
138
- audioflag = True
139
- if not any([textflag, imageflag, audioflag]):
140
- # Raise an error if neither text nor file is provided
141
- raise gr.Error("Enter a valid text, image or audio")
142
- return chatbot
143
-
144
-
145
- def clear_data():
146
- return {"text": None, "image": None, "audio_upload": None, "audio_mic": None, "chatbot": []}
147
-
148
-
149
- def run(history, text, image, audio_upload, audio_mic):
150
- if text in [None, ""]:
151
- text = None
152
-
153
- if audio_upload is not None:
154
- audio = audio_upload
155
- elif audio_mic is not None:
156
- audio = audio_mic
157
- else:
158
- audio = None
159
-
160
- print("text", text)
161
- print("image", image)
162
- print("audio", audio)
163
-
164
- if image is not None:
165
- image = Image.open(image)
166
- outputs = multimodal_phi2(text, audio, image)
167
-
168
- history.append((None, outputs.title()))
169
- return history, None, None, None, None
170
-
171
-
172
- # def print_like_dislike(x: gr.LikeData):
173
- # print(x.index, x.value, x.liked)
174
-
175
-
176
- def add_text(history, text):
177
- history = history + [(text, None)]
178
- return history, gr.Textbox(value="", interactive=False)
179
-
180
-
181
- def add_file(history, file):
182
- history = history + [((file.name,), None)]
183
- return history
184
-
185
-
186
- def bot(history):
187
- response = "**That's cool!**"
188
- history[-1][1] = ""
189
- for character in response:
190
- history[-1][1] += character
191
- time.sleep(0.05)
192
- yield history
193
-
194
-
195
- with gr.Blocks() as demo:
196
- chatbot = gr.Chatbot(
197
- [],
198
- elem_id="chatbot",
199
- bubble_full_width=False,
200
- avatar_images=(None, (os.path.join(os.path.dirname(__file__), "avatar.png"))),
201
- )
202
-
203
- with gr.Row():
204
- txt = gr.Textbox(
205
- scale=4,
206
- show_label=False,
207
- placeholder="Enter text and press enter",
208
- container=False,
209
- )
210
- img_audio = gr.UploadButton("📁", file_types=["image", "audio"], label="Upload Image or Audio")
211
-
212
- with gr.Row():
213
- # Adding a Button
214
- submit = gr.Button()
215
- clear = gr.Button(value="Clear")
216
-
217
- txt_msg = txt.submit(add_input, [chatbot, txt], [chatbot, txt, "text"], queue=False).then(
218
- bot, chatbot, chatbot, api_name="bot_response"
219
- )
220
- img_audio_msg = img_audio.upload(add_input, [chatbot, img_audio], [chatbot, "image"], queue=False).then(
221
- bot, chatbot, chatbot
222
- )
223
-
224
- # submit.click(
225
- # add_content,
226
- # inputs=[chatbot, txt, image, audio_upload, audio_mic],
227
- # outputs=[chatbot],
228
- # ).success(
229
- # run,
230
- # inputs=[chatbot, txt, image, audio_upload, audio_mic],
231
- # outputs=[chatbot, txt, image, audio_upload, audio_mic],
232
- # )
233
-
234
- clear.click(
235
- clear_data,
236
- outputs=[prompt, image, audio_upload, audio_mic, chatbot],
237
- )
238
-
239
- # chatbot.like(print_like_dislike, None, None)
240
-
241
- # demo.queue()
242
  demo.launch()
 
11
  device="cpu",
12
  )
13
 
 
14
  def add_content(chatbot, input_data, input_type) -> gr.Chatbot:
15
  textflag, imageflag, audioflag = False, False, False
16
  if input_type == "text":
 
87
  # chatbot.like(print_like_dislike, None, None)
88
 
89
 
90
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
  demo.launch()