Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,147 +1,61 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
from typing import List, Tuple
|
| 4 |
-
|
| 5 |
-
import aiohttp
|
| 6 |
import panel as pn
|
| 7 |
-
|
| 8 |
-
from transformers import CLIPModel, CLIPProcessor
|
| 9 |
-
|
| 10 |
-
pn.extension(design="bootstrap", sizing_mode="stretch_width")
|
| 11 |
-
|
| 12 |
-
ICON_URLS = {
|
| 13 |
-
"brand-github": "https://github.com/holoviz/panel",
|
| 14 |
-
"brand-twitter": "https://twitter.com/Panel_Org",
|
| 15 |
-
"brand-linkedin": "https://www.linkedin.com/company/panel-org",
|
| 16 |
-
"message-circle": "https://discourse.holoviz.org/",
|
| 17 |
-
"brand-discord": "https://discord.gg/AXRHnJU6sP",
|
| 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 |
-
|
| 30 |
-
|
| 31 |
-
|
| 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 |
-
|
| 39 |
-
|
| 40 |
-
async with session.get(image_url) as resp:
|
| 41 |
-
return Image.open(io.BytesIO(await resp.read()))
|
| 42 |
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 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 |
-
|
| 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 |
-
|
| 102 |
-
randomize_url = pn.widgets.Button(name="Randomize URL", align="end")
|
| 103 |
|
| 104 |
-
|
| 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 |
-
|
| 115 |
-
"##### 😊 Click randomize or paste a URL to start classifying!",
|
| 116 |
-
pn.Row(image_url, randomize_url),
|
| 117 |
-
class_names,
|
| 118 |
-
)
|
| 119 |
-
|
| 120 |
-
# add interactivity
|
| 121 |
-
interactive_result = pn.panel(
|
| 122 |
-
pn.bind(process_inputs, image_url=image_url, class_names=class_names),
|
| 123 |
-
height=600,
|
| 124 |
-
)
|
| 125 |
|
| 126 |
-
|
| 127 |
-
footer_row = pn.Row(pn.Spacer(), align="center")
|
| 128 |
-
for icon, url in ICON_URLS.items():
|
| 129 |
-
href_button = pn.widgets.Button(icon=icon, width=35, height=35)
|
| 130 |
-
href_button.js_on_click(code=f"window.open('{url}')")
|
| 131 |
-
footer_row.append(href_button)
|
| 132 |
-
footer_row.append(pn.Spacer())
|
| 133 |
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
)
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
title=title,
|
| 144 |
-
main=main,
|
| 145 |
-
main_max_width="min(50%, 698px)",
|
| 146 |
-
header_background="#F08080",
|
| 147 |
-
).servable(title=title)
|
|
|
|
| 1 |
+
import hvplot.pandas
|
| 2 |
+
import numpy as np
|
|
|
|
|
|
|
|
|
|
| 3 |
import panel as pn
|
| 4 |
+
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
xs = np.linspace(0, np.pi)
|
| 7 |
|
| 8 |
+
# Widgets pour la fréquence et la phase
|
| 9 |
+
freq = pn.widgets.FloatSlider(name="Frequence", start=0, end=10, value=2)
|
| 10 |
+
phase = pn.widgets.FloatSlider(name="Phase", start=0, end=np.pi)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# Widgets pour la saisie de données personnalisées
|
| 13 |
+
custom_freq_widget = pn.widgets.FloatInput(name="Frequence", value=2)
|
| 14 |
+
custom_phase_widget = pn.widgets.FloatInput(name="Phase", value=0)
|
| 15 |
|
| 16 |
+
def sine(freq, phase):
|
| 17 |
+
return pd.DataFrame(dict(y=np.sin(xs*freq+phase)), index=xs)
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
def cosine(freq, phase):
|
| 20 |
+
return pd.DataFrame(dict(y=np.cos(xs*freq+phase)), index=xs)
|
| 21 |
|
| 22 |
+
dfi_sine = hvplot.bind(sine, freq, phase).interactive()
|
| 23 |
+
dfi_cosine = hvplot.bind(cosine, freq, phase).interactive()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
dfi_custom_sine = hvplot.bind(sine, custom_freq_widget, custom_phase_widget).interactive()
|
| 26 |
+
dfi_custom_cosine = hvplot.bind(cosine, custom_freq_widget, custom_phase_widget).interactive()
|
| 27 |
|
| 28 |
+
plot_opts = dict(responsive=True, min_height=115, min_width=10)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
# Création du tableau de bord
|
| 31 |
+
template = pn.template.ReactTemplate(
|
| 32 |
+
title='Platteforme pour la simulation de tests d’écoute avec le médecin ORL(Oto-Rhino-Laryngologiste et chirurgie de l’oreille)',
|
| 33 |
+
sidebar=[pn.pane.PNG('os-modified.png', width=250),
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
#pn.pane.Markdown("# INTERPRÉTATION GRAPHIQUE DU MODÈLE"),
|
| 37 |
+
#pn.pane.Markdown("# of contention in international "),
|
| 38 |
+
|
| 39 |
+
pn.pane.Markdown("""
|
| 40 |
+
## INTERPRÉTATION GRAPHIQUE DU MODÈLE
|
| 41 |
|
| 42 |
+
Le modèle que vous visualisez ici est basé sur des fonctions sinus et cosinus paramétrées par la fréquence et la phase. Les fonctions sinus et cosinus sont couramment utilisées pour modéliser divers phénomènes périodiques dans la vie pratique.
|
|
|
|
| 43 |
|
| 44 |
+
**Frequency (Fréquence)** : La fréquence contrôle le nombre de cycles complets dans la période [0, π].
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
**Phase (Phase)** : La phase détermine le décalage horizontal (ou la translation) de la fonction par rapport à l'origine.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
En ajustant ces paramètres, vous pouvez observer comment ils affectent les courbes sinus et cosinus. Ces concepts mathématiques sont utilisés pour modéliser des phénomènes périodiques tels que les ondes sonores, les oscillations de pendules, les signaux électriques, etc.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
+
Explorez différentes combinaisons de fréquence et de phase pour mieux comprendre comment ces paramètres influencent les modèles mathématiques utilisés dans la vie pratique.
|
| 51 |
+
""", width=300)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
],
|
| 55 |
)
|
| 56 |
+
# Populate the main area with plots, to demonstrate the grid-like API
|
| 57 |
+
template.main[:3, :6] = pn.Card(dfi_sine.hvplot(**plot_opts).opts(title='Sin'), dfi_cosine.hvplot(**plot_opts).opts(title='Cos'))
|
| 58 |
+
template.main[:3, 6:] = pn.Card(dfi_custom_sine.hvplot(**plot_opts).opts(title='Sin'), dfi_custom_cosine.hvplot(**plot_opts).opts(title='Cos'))
|
| 59 |
|
| 60 |
+
# Lancez le tableau de bord
|
| 61 |
+
template.servable()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|