Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,8 @@
|
|
| 1 |
-
import
|
| 2 |
-
import random
|
| 3 |
-
from typing import List, Tuple
|
| 4 |
-
|
| 5 |
-
import aiohttp
|
| 6 |
import panel as pn
|
| 7 |
-
from
|
| 8 |
-
from transformers import CLIPModel, CLIPProcessor
|
| 9 |
|
| 10 |
-
pn.extension(
|
| 11 |
|
| 12 |
ICON_URLS = {
|
| 13 |
"brand-github": "https://github.com/holoviz/panel",
|
|
@@ -17,110 +12,57 @@ ICON_URLS = {
|
|
| 17 |
"brand-discord": "https://discord.gg/AXRHnJU6sP",
|
| 18 |
}
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 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 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 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 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 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 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
-
#
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
|
|
|
| 124 |
)
|
| 125 |
|
| 126 |
# add footer
|
|
@@ -132,16 +74,14 @@ for icon, url in ICON_URLS.items():
|
|
| 132 |
footer_row.append(pn.Spacer())
|
| 133 |
|
| 134 |
# create dashboard
|
| 135 |
-
main = pn.
|
| 136 |
-
|
| 137 |
-
interactive_result,
|
| 138 |
footer_row,
|
| 139 |
)
|
| 140 |
|
| 141 |
-
title = "
|
| 142 |
-
pn.template.
|
| 143 |
title=title,
|
| 144 |
main=main,
|
| 145 |
-
main_max_width="min(50%, 698px)",
|
| 146 |
header_background="#F08080",
|
| 147 |
-
).servable(title=title)
|
|
|
|
| 1 |
+
import json
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import panel as pn
|
| 3 |
+
from sentrifyai import api
|
|
|
|
| 4 |
|
| 5 |
+
pn.extension(sizing_mode="stretch_width")
|
| 6 |
|
| 7 |
ICON_URLS = {
|
| 8 |
"brand-github": "https://github.com/holoviz/panel",
|
|
|
|
| 12 |
"brand-discord": "https://discord.gg/AXRHnJU6sP",
|
| 13 |
}
|
| 14 |
|
| 15 |
+
async def classify_emotion(message: str):
|
| 16 |
+
emotions = api.Emotions()
|
| 17 |
+
try:
|
| 18 |
+
results = emotions.emotion(model_slug='Emotion-1.0', message=message)
|
| 19 |
+
return results
|
| 20 |
+
except Exception as e:
|
| 21 |
+
return {"error": str(e)}
|
| 22 |
|
| 23 |
+
def process_inputs(message: str):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
try:
|
| 25 |
main.disabled = True
|
| 26 |
+
|
| 27 |
+
# Perform emotion classification
|
| 28 |
+
yield "##### βοΈ Classifying emotions..."
|
| 29 |
+
results = yield from classify_emotion(message)
|
| 30 |
+
|
| 31 |
+
# Display results
|
| 32 |
+
yield "##### π Emotion Classification Results:"
|
| 33 |
+
if "error" in results:
|
| 34 |
+
yield f"Error: {results['error']}"
|
| 35 |
+
else:
|
| 36 |
+
for emotion, score in results.items():
|
| 37 |
+
yield f"{emotion}: {score:.2f}"
|
|
|
|
|
|
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
finally:
|
| 40 |
main.disabled = False
|
| 41 |
|
| 42 |
|
| 43 |
# create widgets
|
| 44 |
+
message_input = pn.widgets.TextInput(
|
| 45 |
+
name="Enter a message for emotion classification",
|
| 46 |
+
placeholder="Type your message here...",
|
| 47 |
+
sizing_mode="stretch_width"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
)
|
| 49 |
|
| 50 |
+
classify_button = pn.widgets.Button(name="Classify Emotion", button_type="primary")
|
| 51 |
+
|
| 52 |
+
# define callback function for button click
|
| 53 |
+
def on_button_click(event):
|
| 54 |
+
message = message_input.value
|
| 55 |
+
if message:
|
| 56 |
+
generator = process_inputs(message)
|
| 57 |
+
panel_content[:] = generator
|
| 58 |
+
|
| 59 |
+
classify_button.on_click(on_button_click)
|
| 60 |
|
| 61 |
+
# create main panel content
|
| 62 |
+
panel_content = pn.Column(
|
| 63 |
+
"### π Emotion Classification",
|
| 64 |
+
message_input,
|
| 65 |
+
classify_button,
|
| 66 |
)
|
| 67 |
|
| 68 |
# add footer
|
|
|
|
| 74 |
footer_row.append(pn.Spacer())
|
| 75 |
|
| 76 |
# create dashboard
|
| 77 |
+
main = pn.Column(
|
| 78 |
+
panel_content,
|
|
|
|
| 79 |
footer_row,
|
| 80 |
)
|
| 81 |
|
| 82 |
+
title = "Emotion Classification"
|
| 83 |
+
pn.template.MaterialTemplate(
|
| 84 |
title=title,
|
| 85 |
main=main,
|
|
|
|
| 86 |
header_background="#F08080",
|
| 87 |
+
).servable(title=title)
|