Spaces:
Sleeping
Sleeping
File size: 6,042 Bytes
915ed4e 5a0c812 915ed4e 5cacb73 915ed4e ecf8366 915ed4e 189a960 915ed4e 5a0c812 832cdec c9e2eaf 35bb3e2 caffc4f 915ed4e bbdadb3 915ed4e bbdadb3 b6dfb35 bbdadb3 c6d5d65 bbdadb3 915ed4e 832cdec bbdadb3 832cdec bbdadb3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | import gradio as gr
from generate_results import format_recipes
from chroma_utils import get_retriever
import os
import zipfile
import gdown
CHROMA_DIR = "./chroma-db-langchain"
ZIP_PATH = "chroma_db.zip"
# Access the secret key
DRIVE_FILE_ID = os.getenv("DRIVE_FILE_ID")
def download_and_extract_chroma():
if not os.path.exists(CHROMA_DIR): # avoid re-download
print("Downloading Chroma DB...")
url = f"https://drive.google.com/uc?id={DRIVE_FILE_ID}"
gdown.download(url, ZIP_PATH, quiet=False)
print("Extracting...")
with zipfile.ZipFile(ZIP_PATH, "r") as zip_ref:
zip_ref.extractall(CHROMA_DIR)
print("Chroma DB ready.")
else:
print("Chroma DB already exists, skipping download.")
# Call this once at the beginning
download_and_extract_chroma()
def get_response(query):
# Call generate_answer_langchain with the retriever and model setup
retriever = get_retriever()
docs = retriever.invoke(query)
raw_context = "\n\n".join([doc.page_content for doc in docs])
response = ""
for chunk in format_recipes(query, raw_context):
response += chunk
yield response
article = "Created with 🤎 (and a mixture of mathematics, statistics, and tons of calculations 👩🏽🔬) by Arpit Vaghela [GitHub](https://github.com/magnifiques)"
example_list = [
["I have tomato and pasta, what should I cook?"],
["I have chicken and potatoes, what can I make?"],
["I have rice and beans, any recipe ideas?"]
]
# Gradio Interface
# demo = gr.Interface(
# fn=get_response, # Function to call
# inputs="text", # Input type
# outputs=gr.Markdown(label="Suggested Recipes"),
# title="WhatToCookToday",
# description="""Welcome to WhatToCookToday! 🍳
# Struggling to decide what to cook with the ingredients you already have?
# we've got you covered.
# Just tell us what’s in your kitchen, and we’ll instantly suggest a delicious recipes you can make with those ingredients.
# You don’t need to search endlessly, we turn your pantry into a personalized recipe book!
# How to use it:
# - Simply type the ingredients you have, like "I have tomatoes and pasta, what can I cook?"
# - You can list multiple ingredients in any order or phrasing.
# - The assistant will respond with a recipe tailored to what you’ve got.
# Example prompts you can try:
# 1. "I have tomato and pasta, what should I cook?"
# 2. "I have chicken and potatoes, what can I make?"
# 3. "I have rice and beans, any recipe ideas?"
# ⏱️ Fast. 🍲 Simple. 🧠 Smart.
# """,
# article=article,
# examples=example_list,
# cache_examples=False,
# live=False
# )
# if __name__ == "__main__":
# demo.launch(
# server_name="0.0.0.0",
# server_port=7860,
# debug=False,
# share=True)
# Create a custom Blocks interface for better control
with gr.Blocks(
title="WhatToCookToday",
theme=gr.themes.Soft(),
css="""
.recipe-output {
max-height: 600px;
overflow-y: auto;
padding: 20px;
border: 1px solid #212121;
border-radius: 8px;
background-color: #0E2148;
}
"""
) as demo:
gr.HTML("""
<div style="text-align: center; padding: 20px;">
<h1>🍳 WhatToCookToday</h1>
<p style="font-size: 18px; color: #666;">
Struggling to decide what to cook with the ingredients you already have? We've got you covered!
</p>
</div>
""")
gr.Markdown("""
**How to use:**
- Simply type the ingredients you have, like "I have tomatoes and pasta, what can I cook?"
- You can list multiple ingredients in any order or phrasing.
- The assistant will respond with a recipe tailored to what you've got.
⏱️ **Fast.** 🍲 **Simple.** 🧠 **Smart.**
""")
with gr.Row():
with gr.Column(scale=2):
input_text = gr.Textbox(
placeholder="Enter your ingredients here... (e.g., 'I have tomatoes and pasta')",
label="What ingredients do you have?",
lines=3
)
submit_btn = gr.Button("🔍 Find Recipes", variant="primary", size="lg")
gr.Examples(
examples=example_list,
inputs=input_text,
label="Try these examples:"
)
with gr.Column(scale=3):
output_markdown = gr.Markdown(
label="Suggested Recipes",
elem_classes=["recipe-output"],
container=True,
value="Your delicious recipes will appear here! 👨🍳"
)
# Add loading behavior
submit_btn.click(
fn=lambda: gr.update(value="🔄 **Searching for recipes...** \n\nPlease wait while we find the perfect recipes for your ingredients!", visible=True),
inputs=None,
outputs=output_markdown,
queue=False
).then(
fn=get_response,
inputs=input_text,
outputs=output_markdown,
show_progress=True
)
# Also allow Enter key submission
input_text.submit(
fn=lambda: gr.update(value="🔄 **Searching for recipes...** \n\nPlease wait while we find the perfect recipes for your ingredients!", visible=True),
inputs=None,
outputs=output_markdown,
queue=False
).then(
fn=get_response,
inputs=input_text,
outputs=output_markdown,
show_progress=True
)
gr.HTML(f"""
<div style="text-align: center; padding: 20px; margin-top: 30px; border-top: 1px solid #eee;">
{article}
</div>
""")
if __name__ == "__main__":
demo.launch(
server_name="0.0.0.0", # Important for HF Spaces
server_port=7860, # Default port for HF Spaces
debug=False,
share=False # Don't use share=True on HF Spaces
) |