Upload 2 files
Browse files- app.py +30 -38
- utils/matcher.py +14 -5
app.py
CHANGED
|
@@ -1,52 +1,44 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
import torch
|
| 4 |
-
from
|
| 5 |
-
import pandas as pd
|
| 6 |
-
from utils.matcher import fuzzy_match
|
| 7 |
|
| 8 |
@st.cache_resource
|
| 9 |
def load_model():
|
| 10 |
-
|
| 11 |
-
model =
|
| 12 |
-
return
|
| 13 |
|
| 14 |
-
|
| 15 |
-
data = pd.read_csv("kaloriedata.csv")
|
| 16 |
-
madliste = data["navn"].tolist()
|
| 17 |
|
| 18 |
-
st.title("
|
| 19 |
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
img = Image.open(uploaded).convert("RGB")
|
| 24 |
-
st.image(img, caption="Uploadet billede", use_container_width=True)
|
| 25 |
-
|
| 26 |
-
inputs = processor(images=img, return_tensors="pt")
|
| 27 |
with torch.no_grad():
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
label = model.config.id2label[class_id.item()]
|
| 32 |
-
confidence = score.item()
|
| 33 |
|
| 34 |
-
|
|
|
|
| 35 |
|
| 36 |
-
if
|
| 37 |
-
|
| 38 |
else:
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
with open("feedback_log.txt", "a") as f:
|
| 51 |
-
f.write(f"{label},{valgt},{confidence:.2f},{feedback}\n")
|
| 52 |
-
st.info("Tak for din feedback! 🙏")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from transformers import AutoFeatureExtractor, AutoModelForImageClassification
|
| 3 |
from PIL import Image
|
| 4 |
import torch
|
| 5 |
+
from utils.matcher import oversæt_fuzzy
|
|
|
|
|
|
|
| 6 |
|
| 7 |
@st.cache_resource
|
| 8 |
def load_model():
|
| 9 |
+
extractor = AutoFeatureExtractor.from_pretrained("nateraw/food101")
|
| 10 |
+
model = AutoModelForImageClassification.from_pretrained("nateraw/food101")
|
| 11 |
+
return extractor, model
|
| 12 |
|
| 13 |
+
extractor, model = load_model()
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
st.title("Kalorieestimering fra billede")
|
| 16 |
|
| 17 |
+
uploaded_file = st.file_uploader("Upload billede", type=["jpg", "png", "jpeg"])
|
| 18 |
+
if uploaded_file:
|
| 19 |
+
image = Image.open(uploaded_file).convert("RGB")
|
| 20 |
+
st.image(image, caption="Uploadet billede", use_column_width=True)
|
| 21 |
|
| 22 |
+
inputs = extractor(images=image, return_tensors="pt")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
with torch.no_grad():
|
| 24 |
+
outputs = model(**inputs)
|
| 25 |
+
probs = torch.nn.functional.softmax(outputs.logits, dim=1)
|
| 26 |
+
confidence, predicted_class = torch.max(probs, dim=1)
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
label = model.config.id2label[predicted_class.item()]
|
| 29 |
+
confidence_pct = confidence.item() * 100
|
| 30 |
|
| 31 |
+
if confidence_pct > 70:
|
| 32 |
+
st.subheader(f"Modelgæt: {label} ({confidence_pct:.1f}%)")
|
| 33 |
else:
|
| 34 |
+
label = st.selectbox("Model usikker – vælg selv fødevare:", list(model.config.id2label.values()))
|
| 35 |
+
st.subheader(f"Valgt: {label}")
|
| 36 |
+
|
| 37 |
+
# Fuzzy matching
|
| 38 |
+
dansk_label = oversæt_fuzzy(label)
|
| 39 |
+
st.write(f"Fortolket som: {dansk_label}")
|
| 40 |
+
|
| 41 |
+
# Feedback
|
| 42 |
+
feedback = st.selectbox("Passede forslaget?", ["Ja", "Nej"])
|
| 43 |
+
st.write("Tak for feedback!")
|
| 44 |
+
|
|
|
|
|
|
|
|
|
utils/matcher.py
CHANGED
|
@@ -1,6 +1,15 @@
|
|
| 1 |
-
from
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from difflib import get_close_matches
|
| 2 |
|
| 3 |
+
# Simple mapping from English label to Danish synonym
|
| 4 |
+
mapping = {
|
| 5 |
+
"french_fries": "pommes frites",
|
| 6 |
+
"pizza": "pizza",
|
| 7 |
+
"ice_cream": "is",
|
| 8 |
+
"apple_pie": "æbletærte",
|
| 9 |
+
"grilled_salmon": "grillet laks"
|
| 10 |
+
# ... tilføj flere som ønsket
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
def oversæt_fuzzy(label):
|
| 14 |
+
match = get_close_matches(label, mapping.keys(), n=1, cutoff=0.5)
|
| 15 |
+
return mapping[match[0]] if match else label
|