Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,147 +1,33 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
async def random_url(_):
|
| 22 |
-
pet = random.choice(["cat", "dog"])
|
| 23 |
-
api_url = f"https://api.the{pet}api.com/v1/images/search"
|
| 24 |
-
async with aiohttp.ClientSession() as session:
|
| 25 |
-
async with session.get(api_url) as resp:
|
| 26 |
-
return (await resp.json())[0]["url"]
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
@pn.cache
|
| 30 |
-
def load_processor_model(
|
| 31 |
-
processor_name: str, model_name: str
|
| 32 |
-
) -> Tuple[CLIPProcessor, CLIPModel]:
|
| 33 |
-
processor = CLIPProcessor.from_pretrained(processor_name)
|
| 34 |
-
model = CLIPModel.from_pretrained(model_name)
|
| 35 |
-
return processor, model
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
async def open_image_url(image_url: str) -> Image:
|
| 39 |
-
async with aiohttp.ClientSession() as session:
|
| 40 |
-
async with session.get(image_url) as resp:
|
| 41 |
-
return Image.open(io.BytesIO(await resp.read()))
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
def get_similarity_scores(class_items: List[str], image: Image) -> List[float]:
|
| 45 |
-
processor, model = load_processor_model(
|
| 46 |
-
"openai/clip-vit-base-patch32", "openai/clip-vit-base-patch32"
|
| 47 |
-
)
|
| 48 |
-
inputs = processor(
|
| 49 |
-
text=class_items,
|
| 50 |
-
images=[image],
|
| 51 |
-
return_tensors="pt", # pytorch tensors
|
| 52 |
-
)
|
| 53 |
-
outputs = model(**inputs)
|
| 54 |
-
logits_per_image = outputs.logits_per_image
|
| 55 |
-
class_likelihoods = logits_per_image.softmax(dim=1).detach().numpy()
|
| 56 |
-
return class_likelihoods[0]
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
async def process_inputs(class_names: List[str], image_url: str):
|
| 60 |
-
"""
|
| 61 |
-
High level function that takes in the user inputs and returns the
|
| 62 |
-
classification results as panel objects.
|
| 63 |
-
"""
|
| 64 |
-
try:
|
| 65 |
-
main.disabled = True
|
| 66 |
-
if not image_url:
|
| 67 |
-
yield "##### ⚠️ Provide an image URL"
|
| 68 |
-
return
|
| 69 |
-
|
| 70 |
-
yield "##### ⚙ Fetching image and running model..."
|
| 71 |
-
try:
|
| 72 |
-
pil_img = await open_image_url(image_url)
|
| 73 |
-
img = pn.pane.Image(pil_img, height=400, align="center")
|
| 74 |
-
except Exception as e:
|
| 75 |
-
yield f"##### 😔 Something went wrong, please try a different URL!"
|
| 76 |
-
return
|
| 77 |
-
|
| 78 |
-
class_items = class_names.split(",")
|
| 79 |
-
class_likelihoods = get_similarity_scores(class_items, pil_img)
|
| 80 |
-
|
| 81 |
-
# build the results column
|
| 82 |
-
results = pn.Column("##### 🎉 Here are the results!", img)
|
| 83 |
-
|
| 84 |
-
for class_item, class_likelihood in zip(class_items, class_likelihoods):
|
| 85 |
-
row_label = pn.widgets.StaticText(
|
| 86 |
-
name=class_item.strip(), value=f"{class_likelihood:.2%}", align="center"
|
| 87 |
-
)
|
| 88 |
-
row_bar = pn.indicators.Progress(
|
| 89 |
-
value=int(class_likelihood * 100),
|
| 90 |
-
sizing_mode="stretch_width",
|
| 91 |
-
bar_color="secondary",
|
| 92 |
-
margin=(0, 10),
|
| 93 |
-
design=pn.theme.Material,
|
| 94 |
-
)
|
| 95 |
-
results.append(pn.Column(row_label, row_bar))
|
| 96 |
-
yield results
|
| 97 |
-
finally:
|
| 98 |
-
main.disabled = False
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
# create widgets
|
| 102 |
-
randomize_url = pn.widgets.Button(name="Randomize URL", align="end")
|
| 103 |
-
|
| 104 |
-
image_url = pn.widgets.TextInput(
|
| 105 |
-
name="Image URL to classify",
|
| 106 |
-
value=pn.bind(random_url, randomize_url),
|
| 107 |
-
)
|
| 108 |
-
class_names = pn.widgets.TextInput(
|
| 109 |
-
name="Comma separated class names",
|
| 110 |
-
placeholder="Enter possible class names, e.g. cat, dog",
|
| 111 |
-
value="cat, dog, parrot",
|
| 112 |
-
)
|
| 113 |
-
|
| 114 |
-
input_widgets = pn.Column(
|
| 115 |
-
"##### 😊 Click randomize or paste a URL to start classifying!",
|
| 116 |
-
pn.Row(image_url, randomize_url),
|
| 117 |
-
class_names,
|
| 118 |
)
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
footer_row.append(href_button)
|
| 132 |
-
footer_row.append(pn.Spacer())
|
| 133 |
-
|
| 134 |
-
# create dashboard
|
| 135 |
-
main = pn.WidgetBox(
|
| 136 |
-
input_widgets,
|
| 137 |
-
interactive_result,
|
| 138 |
-
footer_row,
|
| 139 |
-
)
|
| 140 |
-
|
| 141 |
-
title = "Panel Demo - Image Classification"
|
| 142 |
-
pn.template.BootstrapTemplate(
|
| 143 |
-
title=title,
|
| 144 |
-
main=main,
|
| 145 |
-
main_max_width="min(50%, 698px)",
|
| 146 |
-
header_background="#F08080",
|
| 147 |
-
).servable(title=title)
|
|
|
|
| 1 |
+
from dash import Dash, dcc, html, Input, Output
|
| 2 |
+
|
| 3 |
+
# Create the Dash app
|
| 4 |
+
app = Dash(__name__)
|
| 5 |
+
|
| 6 |
+
# Layout of the app
|
| 7 |
+
app.layout = html.Div([html.H1("✌️يا هلا بالشباب✌️\n"),
|
| 8 |
+
# html.Br(),
|
| 9 |
+
dcc.Input(id='input-text-1', value='ذكر ام انثي؟', type='text'),
|
| 10 |
+
dcc.Input(id='input-text-2', value='Your name', type='text'),
|
| 11 |
+
html.Br(), # line break
|
| 12 |
+
html.Br(),
|
| 13 |
+
html.Div(id='output-text')
|
| 14 |
+
])
|
| 15 |
+
|
| 16 |
+
# Define the callback function
|
| 17 |
+
@app.callback(
|
| 18 |
+
Output('output-text', 'children'), # What gets updated
|
| 19 |
+
Input('input-text-1', 'value'), # First input trigger
|
| 20 |
+
Input('input-text-2', 'value') # Second input trigger
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
)
|
| 22 |
+
def update_output(input1, input2):
|
| 23 |
+
if input1.lower()=="m" or input1.lower()=="male":
|
| 24 |
+
title="Mr"
|
| 25 |
+
elif input1.lower()=="f" or input1.lower()=="female":
|
| 26 |
+
title="Miss"
|
| 27 |
+
else:
|
| 28 |
+
title=" "
|
| 29 |
+
return f'Hello to your 1st web app, {title} {input2} 😊😊😊'
|
| 30 |
+
|
| 31 |
+
# Run the app
|
| 32 |
+
if __name__ == '__main__': # the main() function will execute only if the script is run directly.
|
| 33 |
+
app.run_server(mode="external", host='0.0.0.0', port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|