Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,56 @@
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
from PIL import Image
|
4 |
-
from transformers import BlipProcessor, BlipForConditionalGeneration
|
5 |
import time
|
6 |
|
|
|
7 |
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
|
8 |
-
|
|
|
|
|
|
|
9 |
|
10 |
def caption(img, min_len, max_len):
|
11 |
raw_image = Image.open(img).convert('RGB')
|
12 |
-
|
13 |
inputs = processor(raw_image, return_tensors="pt")
|
14 |
-
|
15 |
-
out = model.generate(**inputs, min_length=min_len, max_length=max_len)
|
16 |
return processor.decode(out[0], skip_special_tokens=True)
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
def greet(img, min_len, max_len):
|
19 |
start = time.time()
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
end = time.time()
|
22 |
total_time = str(end - start)
|
23 |
-
|
|
|
|
|
24 |
return result
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
from PIL import Image
|
4 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration, pipeline
|
5 |
import time
|
6 |
|
7 |
+
# Carregando o modelo BLIP para geração de legendas
|
8 |
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
|
9 |
+
model_blip = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large")
|
10 |
+
|
11 |
+
# Carregando um modelo de geração de texto (exemplo: GPT-2)
|
12 |
+
generator = pipeline('text-generation', model='gpt2')
|
13 |
|
14 |
def caption(img, min_len, max_len):
|
15 |
raw_image = Image.open(img).convert('RGB')
|
|
|
16 |
inputs = processor(raw_image, return_tensors="pt")
|
17 |
+
out = model_blip.generate(**inputs, min_length=min_len, max_length=max_len)
|
|
|
18 |
return processor.decode(out[0], skip_special_tokens=True)
|
19 |
|
20 |
+
def generate_nutritional_info(food_description):
|
21 |
+
# Gerando informações nutricionais com base na descrição do alimento
|
22 |
+
prompt = f"Provide detailed nutritional information about {food_description}."
|
23 |
+
result = generator(prompt, max_length=150, num_return_sequences=1)
|
24 |
+
return result[0]['generated_text']
|
25 |
+
|
26 |
def greet(img, min_len, max_len):
|
27 |
start = time.time()
|
28 |
+
|
29 |
+
# Passo 1: Gerar legenda para a imagem
|
30 |
+
food_description = caption(img, min_len, max_len)
|
31 |
+
|
32 |
+
# Passo 2: Gerar informações nutricionais com base na legenda
|
33 |
+
nutritional_info = generate_nutritional_info(food_description)
|
34 |
+
|
35 |
end = time.time()
|
36 |
total_time = str(end - start)
|
37 |
+
|
38 |
+
# Combinando resultados
|
39 |
+
result = f"Food Description: {food_description}\n\nNutritional Information:\n{nutritional_info}\n\nGenerated in {total_time} seconds."
|
40 |
return result
|
41 |
|
42 |
+
# Interface Gradio
|
43 |
+
iface = gr.Interface(
|
44 |
+
fn=greet,
|
45 |
+
title='Nutritionist Agent with BLIP and GPT-2',
|
46 |
+
description="Upload an image of food, and the agent will describe it and provide nutritional information.",
|
47 |
+
inputs=[
|
48 |
+
gr.Image(type='filepath', label='Image'),
|
49 |
+
gr.Slider(label='Minimum Length', minimum=1, maximum=1000, value=30),
|
50 |
+
gr.Slider(label='Maximum Length', minimum=1, maximum=1000, value=100)
|
51 |
+
],
|
52 |
+
outputs=gr.Textbox(label='Result'),
|
53 |
+
theme=gr.themes.Base(primary_hue="teal", secondary_hue="teal", neutral_hue="slate"),
|
54 |
+
)
|
55 |
+
|
56 |
iface.launch()
|