Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image
|
3 |
+
from transformers import BitsAndBytesConfig, PaliGemmaForConditionalGeneration, PaliGemmaProcessor
|
4 |
+
import spaces
|
5 |
+
import torch
|
6 |
+
import os
|
7 |
+
|
8 |
+
access_token = os.getenv('HF_token')
|
9 |
+
|
10 |
+
model_id = "selamw/BirdWatcher-AI"
|
11 |
+
|
12 |
+
bnb_config = BitsAndBytesConfig(load_in_8bit=True)
|
13 |
+
|
14 |
+
|
15 |
+
def convert_to_markdown(input_text):
|
16 |
+
# Split the input text into sections based on the '**' delimiter
|
17 |
+
sections = input_text.split("**")
|
18 |
+
|
19 |
+
# Initialize the formatted output with the bird name
|
20 |
+
formatted_output = f"**{sections[0].strip()}**\n"
|
21 |
+
|
22 |
+
# Process each section to format it
|
23 |
+
for i in range(1, len(sections), 2):
|
24 |
+
if i + 1 < len(sections):
|
25 |
+
# Use '##' for subheadings and clean up the text
|
26 |
+
header = sections[i].strip() + "** "
|
27 |
+
content = sections[i + 1].strip()
|
28 |
+
formatted_output += f"\n**{header}{content}\n"
|
29 |
+
|
30 |
+
# Return the formatted output
|
31 |
+
return formatted_output.strip()
|
32 |
+
|
33 |
+
|
34 |
+
@spaces.GPU
|
35 |
+
def infer_fin_pali(image, question):
|
36 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
37 |
+
|
38 |
+
model = PaliGemmaForConditionalGeneration.from_pretrained(model_id, quantization_config=bnb_config, token=access_token)
|
39 |
+
processor = PaliGemmaProcessor.from_pretrained(model_id, token=access_token)
|
40 |
+
|
41 |
+
inputs = processor(images=image, text=question, return_tensors="pt").to(device)
|
42 |
+
|
43 |
+
predictions = model.generate(**inputs, max_new_tokens=512)
|
44 |
+
decoded_output = processor.decode(predictions[0], skip_special_tokens=True)[len(question):].lstrip("\n")
|
45 |
+
|
46 |
+
# Ensure proper Markdown formatting
|
47 |
+
formatted_output = convert_to_markdown(decoded_output)
|
48 |
+
|
49 |
+
return formatted_output
|
50 |
+
|
51 |
+
|
52 |
+
css = """
|
53 |
+
#mkd {
|
54 |
+
height: 500px;
|
55 |
+
overflow: auto;
|
56 |
+
border: 1px solid #ccc;
|
57 |
+
}
|
58 |
+
h1 {
|
59 |
+
text-align: center;
|
60 |
+
}
|
61 |
+
h3 {
|
62 |
+
text-align: center;
|
63 |
+
}
|
64 |
+
h2 {
|
65 |
+
text-align: left;
|
66 |
+
}
|
67 |
+
span.gray-text {
|
68 |
+
color: gray;
|
69 |
+
}
|
70 |
+
"""
|
71 |
+
|
72 |
+
with gr.Blocks(css=css) as demo:
|
73 |
+
gr.HTML("<h1>🦩 Bird Identifier: Powered by Fine-tuned PaliGemma 🦜</h1>")
|
74 |
+
gr.HTML("<h3>Upload an image of a bird, and the model will generate a detailed description of its species.</h3>")
|
75 |
+
|
76 |
+
with gr.Tab(label="Bird Identification"):
|
77 |
+
with gr.Row():
|
78 |
+
input_img = gr.Image(label="Input Bird Image")
|
79 |
+
with gr.Column():
|
80 |
+
with gr.Row():
|
81 |
+
question = gr.Text(label="Default Prompt", value="Describe this bird", elem_id="default-prompt")
|
82 |
+
with gr.Row():
|
83 |
+
submit_btn = gr.Button(value="Run")
|
84 |
+
with gr.Row():
|
85 |
+
output = gr.Markdown(label="Response") # Use Markdown component to display output
|
86 |
+
# output = gr.Text(label="Response") # Use Markdown component to display output
|
87 |
+
|
88 |
+
submit_btn.click(infer_fin_pali, [input_img, question], [output])
|
89 |
+
|
90 |
+
gr.Examples(
|
91 |
+
[["020.jpg", "Describe this bird"],
|
92 |
+
["5.jpg", "Describe this bird"],
|
93 |
+
["1.jpg", "Describe this bird"]],
|
94 |
+
inputs=[input_img, question],
|
95 |
+
outputs=[output],
|
96 |
+
fn=infer_fin_pali,
|
97 |
+
label='Examples 👇'
|
98 |
+
)
|
99 |
+
|
100 |
+
demo.launch(debug=True)
|