Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,8 +2,7 @@ import gradio as gr
|
|
| 2 |
from PIL import Image
|
| 3 |
from inference.main import MultiModalPhi2
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
multimodal_phi2 = MultiModalPhi2(
|
| 8 |
modelname_or_path="Navyabhat/Llava-Phi2",
|
| 9 |
temperature=0.2,
|
|
@@ -11,7 +10,38 @@ multimodal_phi2 = MultiModalPhi2(
|
|
| 11 |
device="cpu",
|
| 12 |
)
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
|
|
|
| 15 |
def add_content(chatbot, text, image, audio_upload, audio_mic) -> gr.Chatbot:
|
| 16 |
textflag, imageflag, audioflag = False, False, False
|
| 17 |
if text not in ["", None]:
|
|
@@ -29,14 +59,10 @@ def add_content(chatbot, text, image, audio_upload, audio_mic) -> gr.Chatbot:
|
|
| 29 |
audioflag = True
|
| 30 |
if not any([textflag, imageflag, audioflag]):
|
| 31 |
# Raise an error if neither text nor file is provided
|
| 32 |
-
raise gr.Error("Enter a valid text, image or audio")
|
| 33 |
return chatbot
|
| 34 |
|
| 35 |
-
|
| 36 |
-
def clear_data():
|
| 37 |
-
return {prompt: None, image: None, audio_upload: None, audio_mic: None, chatbot: []}
|
| 38 |
-
|
| 39 |
-
|
| 40 |
def run(history, text, image, audio_upload, audio_mic):
|
| 41 |
if text in [None, ""]:
|
| 42 |
text = None
|
|
@@ -55,61 +81,10 @@ def run(history, text, image, audio_upload, audio_mic):
|
|
| 55 |
if image is not None:
|
| 56 |
image = Image.open(image)
|
| 57 |
outputs = multimodal_phi2(text, audio, image)
|
| 58 |
-
# outputs = ""
|
| 59 |
|
| 60 |
history.append((None, outputs.title()))
|
| 61 |
return history, None, None, None, None
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
gr.Markdown("## MulitModal Phi2 Model Pretraining and Finetuning from Scratch")
|
| 66 |
-
|
| 67 |
-
with gr.Row():
|
| 68 |
-
with gr.Column(scale=4):
|
| 69 |
-
|
| 70 |
-
with gr.Box():
|
| 71 |
-
with gr.Row():
|
| 72 |
-
# Adding a Textbox with a placeholder "write prompt"
|
| 73 |
-
prompt = gr.Textbox(
|
| 74 |
-
placeholder="Ask anything", lines=2, label="Query", value=None
|
| 75 |
-
)
|
| 76 |
-
|
| 77 |
-
with gr.Row():
|
| 78 |
-
# Adding image
|
| 79 |
-
image = gr.Image(type="filepath", value=None)
|
| 80 |
-
|
| 81 |
-
with gr.Row():
|
| 82 |
-
# Add audio
|
| 83 |
-
audio_upload = gr.Audio(source="upload", type="filepath")
|
| 84 |
-
audio_mic = gr.Audio(
|
| 85 |
-
source="microphone", type="filepath", format="mp3"
|
| 86 |
-
)
|
| 87 |
-
|
| 88 |
-
with gr.Column(scale=8):
|
| 89 |
-
with gr.Box():
|
| 90 |
-
with gr.Row():
|
| 91 |
-
chatbot = gr.Chatbot(
|
| 92 |
-
avatar_images=("🧑", "🤖"),
|
| 93 |
-
height=550,
|
| 94 |
-
)
|
| 95 |
-
with gr.Row():
|
| 96 |
-
# Adding a Button
|
| 97 |
-
submit = gr.Button()
|
| 98 |
-
clear = gr.Button(value="Clear")
|
| 99 |
-
|
| 100 |
-
submit.click(
|
| 101 |
-
add_content,
|
| 102 |
-
inputs=[chatbot, prompt, image, audio_upload, audio_mic],
|
| 103 |
-
outputs=[chatbot],
|
| 104 |
-
).success(
|
| 105 |
-
run,
|
| 106 |
-
inputs=[chatbot, prompt, image, audio_upload, audio_mic],
|
| 107 |
-
outputs=[chatbot, prompt, image, audio_upload, audio_mic],
|
| 108 |
-
)
|
| 109 |
-
|
| 110 |
-
clear.click(
|
| 111 |
-
clear_data,
|
| 112 |
-
outputs=[prompt, image, audio_upload, audio_mic, chatbot],
|
| 113 |
-
)
|
| 114 |
-
|
| 115 |
demo.launch()
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
from inference.main import MultiModalPhi2
|
| 4 |
|
| 5 |
+
# Initialize the chatbot model
|
|
|
|
| 6 |
multimodal_phi2 = MultiModalPhi2(
|
| 7 |
modelname_or_path="Navyabhat/Llava-Phi2",
|
| 8 |
temperature=0.2,
|
|
|
|
| 10 |
device="cpu",
|
| 11 |
)
|
| 12 |
|
| 13 |
+
# Initialize chatbot history
|
| 14 |
+
messages = []
|
| 15 |
+
|
| 16 |
+
# UI setup
|
| 17 |
+
with gr.Blocks() as demo:
|
| 18 |
+
chatbot = gr.Chatbot(
|
| 19 |
+
[],
|
| 20 |
+
elem_id="chatbot",
|
| 21 |
+
bubble_full_width=False,
|
| 22 |
+
avatar_images=(None, None), # You can specify avatar images if needed
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
with gr.Row():
|
| 26 |
+
txt = gr.Textbox(
|
| 27 |
+
scale=4,
|
| 28 |
+
show_label=False,
|
| 29 |
+
placeholder="Enter text and press enter, or upload an image",
|
| 30 |
+
container=False,
|
| 31 |
+
)
|
| 32 |
+
btn = gr.UploadButton("📁", file_types=["image", "video", "audio"])
|
| 33 |
+
|
| 34 |
+
txt_msg = txt.submit(add_content, [chatbot, txt], [chatbot, txt], queue=False).then(
|
| 35 |
+
run, [chatbot, txt, None, None, None], [chatbot, txt, None, None, None], api_name="bot_response"
|
| 36 |
+
)
|
| 37 |
+
txt_msg.then(lambda: gr.Textbox(interactive=True), None, [txt], queue=False)
|
| 38 |
+
file_msg = btn.upload(add_content, [chatbot, None, None, btn, None], [chatbot, None, None, None], queue=False).then(
|
| 39 |
+
run, [chatbot, None, None, None, btn], [chatbot, None, None, None, None]
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
chatbot.like(print_like_dislike, None, None)
|
| 43 |
|
| 44 |
+
# Function to add content to chatbot
|
| 45 |
def add_content(chatbot, text, image, audio_upload, audio_mic) -> gr.Chatbot:
|
| 46 |
textflag, imageflag, audioflag = False, False, False
|
| 47 |
if text not in ["", None]:
|
|
|
|
| 59 |
audioflag = True
|
| 60 |
if not any([textflag, imageflag, audioflag]):
|
| 61 |
# Raise an error if neither text nor file is provided
|
| 62 |
+
raise gr.Error("Enter a valid text, image, or audio")
|
| 63 |
return chatbot
|
| 64 |
|
| 65 |
+
# Function to run the chatbot
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
def run(history, text, image, audio_upload, audio_mic):
|
| 67 |
if text in [None, ""]:
|
| 68 |
text = None
|
|
|
|
| 81 |
if image is not None:
|
| 82 |
image = Image.open(image)
|
| 83 |
outputs = multimodal_phi2(text, audio, image)
|
|
|
|
| 84 |
|
| 85 |
history.append((None, outputs.title()))
|
| 86 |
return history, None, None, None, None
|
| 87 |
|
| 88 |
+
# Launch the Gradio UI
|
| 89 |
+
demo.queue()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
demo.launch()
|