Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
import requests
|
| 3 |
+
import io
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import tempfile
|
| 7 |
+
|
| 8 |
+
recipe_generator = pipeline("text2text-generation", model="flax-community/t5-recipe-generation")
|
| 9 |
+
|
| 10 |
+
API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
|
| 11 |
+
headers = {'Authorization': f'Bearer a'}
|
| 12 |
+
|
| 13 |
+
def query(payload):
|
| 14 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 15 |
+
return response.content
|
| 16 |
+
|
| 17 |
+
def generate(input):
|
| 18 |
+
inputs = [f"items: {item}" for item in [input]]
|
| 19 |
+
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)
|
| 20 |
+
recipe_text = generated_recipes[0]['generated_text']
|
| 21 |
+
|
| 22 |
+
#make recipe better into json?
|
| 23 |
+
image_bytes = query({
|
| 24 |
+
"inputs": str(recipe_text),
|
| 25 |
+
})
|
| 26 |
+
image = Image.open(io.BytesIO(image_bytes))
|
| 27 |
+
|
| 28 |
+
temp_file = tempfile.NamedTemporaryFile(suffix=".jpg", delete=False)
|
| 29 |
+
image.save(temp_file, format="JPEG")
|
| 30 |
+
temp_file.close()
|
| 31 |
+
|
| 32 |
+
return recipe_text, temp_file.name
|
| 33 |
+
|
| 34 |
+
demo = gr.Interface(
|
| 35 |
+
fn=generate, # Function to process input
|
| 36 |
+
inputs=gr.Textbox(label="Enter ingredients separated by commas"), # Text input on the left
|
| 37 |
+
outputs=[gr.Textbox(label="Recipe Display"), gr.Image(label="Recipe Image")]
|
| 38 |
+
)
|
| 39 |
+
demo.launch(share=True)
|