Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,4 @@
|
|
1 |
import gradio as gr
|
2 |
-
import requests
|
3 |
from PIL import Image
|
4 |
from transformers import BlipProcessor, BlipForConditionalGeneration, pipeline
|
5 |
import time
|
@@ -11,32 +10,42 @@ model_blip = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image
|
|
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 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
23 |
result = generator(prompt, max_length=150, num_return_sequences=1)
|
24 |
return result[0]['generated_text']
|
25 |
|
26 |
-
|
|
|
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 |
-
|
|
|
|
|
|
|
|
|
40 |
return result
|
41 |
|
42 |
# Interface Gradio
|
@@ -47,7 +56,8 @@ iface = gr.Interface(
|
|
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"),
|
|
|
1 |
import gradio as gr
|
|
|
2 |
from PIL import Image
|
3 |
from transformers import BlipProcessor, BlipForConditionalGeneration, pipeline
|
4 |
import time
|
|
|
10 |
# Carregando um modelo de geração de texto (exemplo: GPT-2)
|
11 |
generator = pipeline('text-generation', model='gpt2')
|
12 |
|
13 |
+
# Função para gerar legenda da imagem
|
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 |
+
# Função para gerar informações nutricionais e calorias
|
21 |
+
def generate_nutritional_info(food_description, language):
|
22 |
+
if language == "Português":
|
23 |
+
prompt = f"Descreva as informações nutricionais e as calorias do seguinte alimento: {food_description}."
|
24 |
+
else:
|
25 |
+
prompt = f"Provide detailed nutritional information and calories for the following food: {food_description}."
|
26 |
+
|
27 |
result = generator(prompt, max_length=150, num_return_sequences=1)
|
28 |
return result[0]['generated_text']
|
29 |
|
30 |
+
# Função principal que combina tudo
|
31 |
+
def greet(img, min_len, max_len, language):
|
32 |
start = time.time()
|
33 |
|
34 |
# Passo 1: Gerar legenda para a imagem
|
35 |
food_description = caption(img, min_len, max_len)
|
36 |
|
37 |
+
# Passo 2: Gerar informações nutricionais e calorias com base na legenda
|
38 |
+
nutritional_info = generate_nutritional_info(food_description, language)
|
39 |
|
40 |
end = time.time()
|
41 |
total_time = str(end - start)
|
42 |
|
43 |
# Combinando resultados
|
44 |
+
if language == "Português":
|
45 |
+
result = f"Descrição do Alimento: {food_description}\n\nInformações Nutricionais:\n{nutritional_info}\n\nGerado em {total_time} segundos."
|
46 |
+
else:
|
47 |
+
result = f"Food Description: {food_description}\n\nNutritional Information:\n{nutritional_info}\n\nGenerated in {total_time} seconds."
|
48 |
+
|
49 |
return result
|
50 |
|
51 |
# Interface Gradio
|
|
|
56 |
inputs=[
|
57 |
gr.Image(type='filepath', label='Image'),
|
58 |
gr.Slider(label='Minimum Length', minimum=1, maximum=1000, value=30),
|
59 |
+
gr.Slider(label='Maximum Length', minimum=1, maximum=1000, value=100),
|
60 |
+
gr.Radio(choices=["Português", "English"], label="Language", value="Português") # Botão de seleção de idioma
|
61 |
],
|
62 |
outputs=gr.Textbox(label='Result'),
|
63 |
theme=gr.themes.Base(primary_hue="teal", secondary_hue="teal", neutral_hue="slate"),
|