ajdarshaydullin commited on
Commit
c725fc9
·
1 Parent(s): 6f2e64e

Add application file

Browse files
Files changed (1) hide show
  1. app.py +57 -4
app.py CHANGED
@@ -1,8 +1,61 @@
1
  import gradio as gr
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- demo.launch()
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
 
3
+ # Use a pipeline as a high-level helper
4
+ from transformers import pipeline
5
 
6
+ detector = pipeline("object-detection", model="hustvl/yolos-tiny")
 
7
 
8
+ # Load model directly
9
+ from transformers import AutoImageProcessor, AutoModelForImageClassification
10
+
11
+ processor = AutoImageProcessor.from_pretrained("microsoft/resnet-50")
12
+ model = AutoModelForImageClassification.from_pretrained("microsoft/resnet-50")
13
+ def food_classifier(image):
14
+ inputs = processor(image, return_tensors="pt")
15
+ logits = model(**inputs).logits
16
+ predicted_label = logits.argmax(-1).item()
17
+ label = model.config.id2label[predicted_label]
18
+ return [{'label': label, 'score': 0.0}]
19
+
20
+
21
+ # Use a pipeline as a high-level helper
22
+ from transformers import pipeline
23
+
24
+ food_classifier = pipeline("image-classification", model="facebook/deit-base-distilled-patch16-384")
25
+
26
+ def get_ingridients_list(image, score_threshold=.85):
27
+ objects = detector(image)
28
+ ingridients = []
29
+ for obj in objects:
30
+ cropped_image = image.crop((obj['box']['xmin'], obj['box']['ymin'], obj['box']['xmax'], obj['box']['ymax']))
31
+ classes = food_classifier(cropped_image)
32
+ best_match = max(classes, key=lambda x: x['score'])
33
+ if best_match['score'] > score_threshold:
34
+ ingridients.append(best_match['label'])
35
+ return list(set(ingridients))
36
+
37
+ def get_ingridients(image):
38
+ ingridients = get_ingridients_list(image)
39
+ return ', '.join(ingridients)
40
+
41
+
42
+ #text_to_text = pipeline("text-generation", model="ai-forever/mGPT")
43
+
44
+ def get_reciepe(ingridients):
45
+ return 'dish of ' + ingridients
46
+
47
+ def get_answer(image):
48
+ ingridients = get_ingridients(image)
49
+ return get_reciepe(ingridients)
50
+
51
+ # Create a Gradio interface
52
+ iface = gr.Interface(
53
+ fn=get_answer, # Function to call
54
+ inputs=gr.Image(label="Upload an image", type="pil"), # Input type: Image
55
+ outputs=gr.Markdown(label="Classification Result"), # Output type: Markdown
56
+ title="Food Ingredient Classifier",
57
+ description="Upload an image of a food ingredient to classify it."
58
+ )
59
+
60
+ # Launch the Gradio app
61
+ iface.launch()