Spaces:
Sleeping
Sleeping
import PIL.Image | |
import gradio as gr | |
import base64 | |
import os | |
import google.generativeai as genai | |
from gradio import ClearButton | |
# Set Google API key | |
genai.configure(api_key = os.environ['GOOGLE_API_KEY']) | |
# Create the Model | |
txt_model = genai.GenerativeModel('gemini-pro') | |
vis_model = genai.GenerativeModel('gemini-pro-vision') | |
# Image to Base 64 Converter | |
def image_to_base64(image_path): | |
with open(image_path, 'rb') as img: | |
encoded_string = base64.b64encode(img.read()) | |
return encoded_string.decode('utf-8') | |
# Function that takes User Inputs and displays it on ChatUI | |
def query_message(history,txt,img): | |
if not img: | |
history += [(txt,None)] | |
return history | |
base64 = image_to_base64(img) | |
data_url = f"data:image/jpeg;base64,{base64}" | |
history += [(f"{txt} ![]({data_url})", None)] | |
return history | |
# Function that takes User Inputs, generates Response and displays on Chat UI | |
def llm_response(history,text,img): | |
if not img: | |
response = txt_model.generate_content(text) | |
history += [(None,response.text)] | |
return history | |
else: | |
img = PIL.Image.open(img) | |
response = vis_model.generate_content([text,img]) | |
history += [(None,response.text)] | |
return history | |
def get_user_message(): | |
return ("Craft a weekly menu, Monday - Sunday (without dates) in style of the attached image, presenting the entire menu for each day one after the other, vertically, each celebrating fusion cuisine, blending elements from different culinary traditions into innovative dishes. Each day, present a chicken or fish or beef/pork, and vegetarian main dish that combines flavors and techniques from two or more cuisines. Accompany these with creatively paired side dishes (including rice and no rice options), salads that merge diverse ingredients harmoniously, and desserts that are a fusion of traditional treats. Additionally, each day offer a fusion-inspired soup of the day or salad as a complete meal choice, ensuring a delightful and surprising blend of tastes and textures.") | |
# Interface Code | |
with gr.Blocks() as app: | |
# Markdown block for title and description | |
gr.Markdown("# HOW TO USE THIS AI MENU GENERATOR") | |
gr.Markdown("1. Copy an entire Excel sheet of Maggies menu data, paste into MSPaint, save the resulting image on your PC system or desktop, and then upload it to area below.\n\n" | |
"2. Click 'Generate Menu' to send the image to the AI menu generator for processing.\n\n" | |
"3. Copy and paste the generated menus into the Excel document where they are kept.\n\n" | |
"4. CUSTOMIZE or EDIT items in any generated menu based on your own knowledge and judgement as a chef or cook about what will work best for Maggie's kitchen and customers.\n\n" | |
"5. Click 'Clear' to upload other historical menu data to generate new menus.\n\n" | |
"<strong>Note:</strong>. If the model outputs an 'Error' or takes longer than 2 minutes to generate an answer, just click the refresh button on your browser bar or page and restart your image upload process.") | |
with gr.Row(): | |
image_box = gr.Image(type="filepath") | |
chatbot = gr.Chatbot( | |
scale=2, | |
height=750 | |
) | |
text_box = gr.Textbox( | |
value=get_user_message, # Set value to a callable function | |
interactive=False, # Disable editing | |
container=False, | |
) | |
btn = gr.Button("Generate Menu") | |
# Create a ClearButton instance with the components to clear | |
clear_btn = ClearButton([chatbot, image_box]) | |
clicked = btn.click(query_message, | |
[chatbot,text_box,image_box], | |
chatbot | |
).then(llm_response, | |
[chatbot,text_box,image_box], | |
chatbot | |
) | |
app.queue() | |
app.launch(debug=True) |