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