Spaces:
Build error
Build error
dafajudin
commited on
Commit
·
d5c337b
1
Parent(s):
5e5ca2a
update code
Browse files
app.py
CHANGED
|
@@ -1,25 +1,102 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
|
|
|
| 3 |
|
|
|
|
| 4 |
generator = pipeline("visual-question-answering", model="jihadzakki/blip1-medvqa")
|
| 5 |
|
| 6 |
-
def format_answer(image, question):
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
)
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
+
import plotly.graph_objs as go
|
| 4 |
|
| 5 |
+
# Load the Visual QA model
|
| 6 |
generator = pipeline("visual-question-answering", model="jihadzakki/blip1-medvqa")
|
| 7 |
|
| 8 |
+
def format_answer(image, question, history):
|
| 9 |
+
try:
|
| 10 |
+
result = generator(image, question, max_new_tokens=50)
|
| 11 |
+
predicted_answer = result[0].get('answer', 'No answer found')
|
| 12 |
+
history.append(f"Question: {question} | Answer: {predicted_answer}")
|
| 13 |
+
# Create a simple chart for demonstration purposes
|
| 14 |
+
chart = create_chart(predicted_answer)
|
| 15 |
+
return f"Predicted Answer: {predicted_answer}", chart, history
|
| 16 |
+
except Exception as e:
|
| 17 |
+
return f"Error: {str(e)}", None, history
|
| 18 |
+
|
| 19 |
+
def create_chart(predicted_answer):
|
| 20 |
+
# Example chart data
|
| 21 |
+
labels = ['Positive', 'Negative']
|
| 22 |
+
values = [1, 0] if predicted_answer.lower() == 'yes' else [0, 1]
|
| 23 |
+
colors = ['blue', 'red'] if predicted_answer.lower() == 'yes' else ['red', 'blue']
|
| 24 |
+
|
| 25 |
+
fig = go.Figure(data=[go.Pie(labels=labels, values=values, marker=dict(colors=colors))])
|
| 26 |
+
return fig
|
| 27 |
+
|
| 28 |
+
def switch_theme(mode):
|
| 29 |
+
if mode == "Light Mode":
|
| 30 |
+
return gr.themes.Default()
|
| 31 |
+
else:
|
| 32 |
+
return gr.themes.Soft(primary_hue=gr.themes.colors.blue, secondary_hue=gr.themes.colors.orange)
|
| 33 |
+
|
| 34 |
+
def save_feedback(feedback):
|
| 35 |
+
return "Thank you for your feedback!"
|
| 36 |
+
|
| 37 |
+
# Build the Visual QA application using Gradio with improvements
|
| 38 |
+
with gr.Blocks(
|
| 39 |
+
theme=gr.themes.Soft(
|
| 40 |
+
font=[gr.themes.GoogleFont("Inconsolata"), "Arial", "sans-serif"],
|
| 41 |
+
primary_hue=gr.themes.colors.blue,
|
| 42 |
+
secondary_hue=gr.themes.colors.red,
|
| 43 |
+
)
|
| 44 |
+
) as VisualQAApp:
|
| 45 |
+
gr.Markdown("# Visual Question Answering using BLIP Model", elem_classes="title")
|
| 46 |
+
|
| 47 |
+
with gr.Row():
|
| 48 |
+
with gr.Column():
|
| 49 |
+
image_input = gr.Image(label="Upload image", type="pil")
|
| 50 |
+
question_input = gr.Textbox(show_label=False, placeholder="Enter your question here...")
|
| 51 |
+
submit_button = gr.Button("Submit", variant="primary")
|
| 52 |
+
|
| 53 |
+
with gr.Column():
|
| 54 |
+
answer_output = gr.Textbox(label="Result Prediction")
|
| 55 |
+
chart_output = gr.Plot(label="Interactive Chart")
|
| 56 |
+
|
| 57 |
+
history_state = gr.State([]) # Initialize the history state
|
| 58 |
+
|
| 59 |
+
submit_button.click(
|
| 60 |
+
format_answer,
|
| 61 |
+
inputs=[image_input, question_input, history_state],
|
| 62 |
+
outputs=[answer_output, chart_output, history_state],
|
| 63 |
+
show_progress=True
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
with gr.Row():
|
| 67 |
+
history = gr.Textbox(label="History Log", lines=10)
|
| 68 |
+
gr.Markdown("**Log of previous interactions:**")
|
| 69 |
+
submit_button.click(
|
| 70 |
+
lambda history: "\n".join(history),
|
| 71 |
+
inputs=[history_state],
|
| 72 |
+
outputs=[history]
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
with gr.Accordion("Help", open=False):
|
| 76 |
+
gr.Markdown("**Upload image**: Select the chest X-ray image you want to analyze.")
|
| 77 |
+
gr.Markdown("**Enter your question**: Type the question you have about the image, such as 'Is there any sign of pneumonia?'")
|
| 78 |
+
gr.Markdown("**Submit**: Click the submit button to get the prediction from the model.")
|
| 79 |
+
|
| 80 |
+
with gr.Accordion("User Preferences", open=False):
|
| 81 |
+
gr.Markdown("**Mode**: Choose between light and dark mode for your comfort.")
|
| 82 |
+
mode_selector = gr.Radio(choices=["Light Mode", "Dark Mode"], label="Select Mode")
|
| 83 |
+
apply_theme_button = gr.Button("Apply Theme")
|
| 84 |
+
|
| 85 |
+
apply_theme_button.click(
|
| 86 |
+
switch_theme,
|
| 87 |
+
inputs=[mode_selector],
|
| 88 |
+
outputs=[],
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
with gr.Accordion("Feedback", open=False):
|
| 92 |
+
gr.Markdown("**We value your feedback!** Please provide any feedback you have about this application.")
|
| 93 |
+
feedback_input = gr.Textbox(label="Feedback", lines=4)
|
| 94 |
+
submit_feedback_button = gr.Button("Submit Feedback")
|
| 95 |
+
|
| 96 |
+
submit_feedback_button.click(
|
| 97 |
+
save_feedback,
|
| 98 |
+
inputs=[feedback_input],
|
| 99 |
+
outputs=[feedback_input]
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
VisualQAApp.launch(share=True, server_name="0.0.0.0", server_port=8080, debug=True)
|