tonyliu404's picture
Update app.py
46e989f verified
from transformers import pipeline
import requests
import io
from PIL import Image
import gradio as gr
import tempfile
import os
HF = os.getenv("HF_SECRET")
recipe_generator = pipeline("text2text-generation", model="flax-community/t5-recipe-generation")
API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
headers = {'Authorization': f'Bearer {HF}'}
def query(payload):
response = requests.post(API_URL, headers=headers, json=payload)
return response.content
def generate(input):
inputs = [f"items: {item}" for item in [input]]
generated_recipes = recipe_generator(inputs, max_length=512, min_length=64, no_repeat_ngram_size=3, do_sample=True, top_k=60, top_p=0.95)
recipe_text = generated_recipes[0]['generated_text']
#make recipe better into json?
image_bytes = query({
"inputs": str(recipe_text),
})
image = Image.open(io.BytesIO(image_bytes))
temp_file = tempfile.NamedTemporaryFile(suffix=".jpg", delete=False)
image.save(temp_file, format="JPEG")
temp_file.close()
return recipe_text, temp_file.name
demo = gr.Interface(
fn=generate, # Function to process input
inputs=gr.Textbox(label="Enter ingredients separated by commas"), # Text input on the left
outputs=[gr.Textbox(label="Recipe Display"), gr.Image(label="Recipe Image")]
)
demo.launch()