Spaces:
Sleeping
Sleeping
Commit ·
2759f88
1
Parent(s): b1f272d
initial commit
Browse files12 unmodified lines13141516171819202122232 unmodified lines262728293031325 unmodified lines38394041424344454647484950515253545556575859606162636465666768692 unmodified lines7273747576777879808182838485868788899091929331 unmodified lines12512612712812913013113213313413513613713813914014114214314411 unmodified lines1561571581591601612 unmodified lines16416516616716816917029 unmodified lines20020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125212 unmodified linesinitial_sidebar_state="expanded",)# Custom CSS to improve styling and responsivenessdef local_css():st.markdown("""<style>/* Main layout */.main {background-color: #f0f2f6;2 unmodified lines.title h1 {font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;text-align: center;color: #ff4b4b;font-size: 3rem;margin-bottom: 20px;}5 unmodified lines}/* Sidebar styling */[data-testid="stSidebar"] {background-color: #ff4b4b;}[data-testid="stSidebar"] .css-ng1t4o {color: white;}[data-testid="stSidebar"] .css-1d391kg {color: white;}/* File uploader styling */.stFileUploader {border: 2px dashed #ff4b4b;border-radius: 10px;padding: 20px;text-align: center;color: #ff4b4b;background-color: #ffffff;font-weight: bold;}/* File uploader hover effect */.stFileUploader:hover {background-color: #ffe5e5;}/* Button styling */.stButton>button {background-color: #ff4b4b;color: white;border: none;padding: 0.7rem 1.5rem;border-radius: 5px;2 unmodified linesmargin-top: 10px;}.stButton>button:hover {background-color: #e04343;color: white;}/* Headers styling */h2 {color: #ff4b4b;margin-top: 30px;font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;}h3 {color: #ff4b4b;font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;}/* Text styling */.stMarkdown p {font-size: 1.1rem;}/* Footer styling */footer {31 unmodified linesborder: 2px solid transparent;}.sample-images img:hover {border: 2px solid #ff4b4b;}</style>""",unsafe_allow_html=True)local_css()# Hugging Face API keyAPI_KEY = st.secrets["HF_API_KEY"]# Initialize the Hugging Face Inference Clientclient = InferenceClient(api_key=API_KEY)# Load the image classification pipeline@st.cache_resource11 unmodified linesGenerate a list of ingredients for the given food item using Qwen NLP model.Returns a clean, comma-separated list of ingredients."""messages = [{"role": "user",2 unmodified lines}]try:completion = client.chat.completions.create(model="Qwen/Qwen2.5-Coder-32B-Instruct",messages=messages,max_tokens=5029 unmodified lines}cols = st.columns(len(sample_images))for idx, (name, file_path) in enumerate(sample_images.items()):with cols[idx]:if st.button(f"{name}", key=name):uploaded_file = file_path# File uploaderst.subheader("Upload a food image:")uploaded_file = st.file_uploader("", type=["jpg", "png", "jpeg"])if 'uploaded_file' in locals() and uploaded_file is not None:# Display the uploaded imageif isinstance(uploaded_file, str):# Sample image selectedimage = Image.open(uploaded_file)else:# User uploaded imageimage = Image.open(uploaded_file)st.image(image, caption="Uploaded Image", use_container_width=True)# Classification buttonif st.button("Classify"):with st.spinner("Classifying..."):# Make predictionspredictions = pipe_classification(image)# Display only the top predictiontop_food = predictions[0]['label']st.header(f"🍽️ Food: {top_food}")# Generate and display ingredients for the top predictionst.subheader("📝 Ingredients")try:ingredients = get_ingredients_qwen(top_food)st.write(ingredients)except Exception as e:st.error(f"Error generating ingredients: {e}")st.subheader("💡 Healthier Alternatives")try:client_gradio = Client("https://8a56cb969da1f9d721.gradio.live/")result = client_gradio.predict(query=f"What's a healthy {top_food} recipe, and why is it healthy?",api_name="/get_response")st.write(result)except Exception as e:st.error(f"Unable to contact RAG: {e}")else:st.info("Please select or upload an image to get started.")12 unmodified lines1314151617181920212223242526272829303132332 unmodified lines363738394041425 unmodified lines48495051525354555657585960616263646566676869707172737475767778792 unmodified lines82838485868788899091929394959697989910010110210310431 unmodified lines13613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326411 unmodified lines2762772782792802812822832 unmodified lines28628728828929029129229 unmodified lines32232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437512 unmodified linesinitial_sidebar_state="expanded",)# Initialize mode in session stateif 'mode' not in st.session_state:st.session_state['mode'] = 'Light Mode'# Light/Dark mode togglemode = st.sidebar.selectbox('Select Mode', options=['Light Mode', 'Dark Mode'], index=0 if st.session_state['mode'] == 'Light Mode' else 1)st.session_state['mode'] = mode# Custom CSS to improve styling and responsivenessdef local_css(mode='Light Mode'):if mode == 'Light Mode':css_string = '''<style>/* Light mode CSS *//* Main layout */.main {background-color: #f0f2f6;2 unmodified lines.title h1 {font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;text-align: center;color: #000000; /* Black color */font-size: 3rem;margin-bottom: 20px;}5 unmodified lines}/* Sidebar styling */[data-testid="stSidebar"] {background-color: #ffffff; /* White color */}[data-testid="stSidebar"] .css-ng1t4o {color: #000000; /* Black color */}[data-testid="stSidebar"] .css-1d391kg {color: #000000; /* Black color */}/* File uploader styling */.stFileUploader {border: 2px dashed #000000; /* Black color */border-radius: 10px;padding: 20px;text-align: center;color: #000000; /* Black color */background-color: #ffffff; /* White background */font-weight: bold;}/* File uploader hover effect */.stFileUploader:hover {background-color: #e0e0e0;}/* Button styling */.stButton>button {background-color: #000000; /* Black background */color: #ffffff; /* White text */border: none;padding: 0.7rem 1.5rem;border-radius: 5px;2 unmodified linesmargin-top: 10px;}.stButton>button:hover {background-color: #333333; /* Darker black */color: #ffffff;}/* Headers styling */h2 {color: #000000; /* Black color */margin-top: 30px;font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;}h3 {color: #000000; /* Black color */font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;}/* Text styling */.stMarkdown p {font-size: 1.1rem;color: #000000;}/* Footer styling */footer {31 unmodified linesborder: 2px solid transparent;}.sample-images img:hover {border: 2px solid #000000; /* Black border on hover */}</style>'''else:css_string = '''<style>/* Dark mode CSS *//* Main layout */.main {background-color: #1e1e1e;}/* Title styling */.title h1 {font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;text-align: center;color: #ffffff; /* White color */font-size: 3rem;margin-bottom: 20px;}/* Image styling */.st-image img {border-radius: 15px;margin-bottom: 20px;max-width: 100%;}/* Sidebar styling */[data-testid="stSidebar"] {background-color: #333333; /* Dark gray */}[data-testid="stSidebar"] .css-ng1t4o {color: #ffffff; /* White color */}[data-testid="stSidebar"] .css-1d391kg {color: #ffffff; /* White color */}/* File uploader styling */.stFileUploader {border: 2px dashed #ffffff; /* White color */border-radius: 10px;padding: 20px;text-align: center;color: #ffffff; /* White color */background-color: #1e1e1e; /* Dark background */font-weight: bold;}/* File uploader hover effect */.stFileUploader:hover {background-color: #333333;}/* Button styling */.stButton>button {background-color: #ffffff; /* White background */color: #000000; /* Black text */border: none;padding: 0.7rem 1.5rem;border-radius: 5px;font-size: 1.1rem;font-weight: bold;margin-top: 10px;}.stButton>button:hover {background-color: #e0e0e0; /* Lighter gray */color: #000000;}/* Headers styling */h2 {color: #ffffff; /* White color */margin-top: 30px;font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;}h3 {color: #ffffff; /* White color */font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;}/* Text styling */.stMarkdown p {font-size: 1.1rem;color: #ffffff; /* White text */}/* Footer styling */footer {visibility: hidden;}/* Hide sidebar on small screens */@media only screen and (max-width: 600px) {[data-testid="stSidebar"] {display: none;}.main .block-container {padding-left: 1rem;padding-right: 1rem;}.title h1 {font-size: 2rem;}.stButton>button {width: 100%;}}/* Sample images grid */.sample-images {display: flex;justify-content: center;flex-wrap: wrap;gap: 10px;}.sample-images img {width: 150px;height: 150px;object-fit: cover;border-radius: 10px;cursor: pointer;border: 2px solid transparent;}.sample-images img:hover {border: 2px solid #ffffff; /* White border on hover */}</style>'''st.markdown(css_string, unsafe_allow_html=True)local_css(st.session_state['mode'])# Load the image classification pipeline@st.cache_resource11 unmodified linesGenerate a list of ingredients for the given food item using Qwen NLP model.Returns a clean, comma-separated list of ingredients."""API_KEY = st.secrets["HF_API_KEY"]client = InferenceClient(api_key=API_KEY)messages = [{"role": "user",2 unmodified lines}]try:completion = client.chat_completions.create(model="Qwen/Qwen2.5-Coder-32B-Instruct",messages=messages,max_tokens=5029 unmodified lines}cols = st.columns(len(sample_images))selected_sample = Nonefor idx, (name, file_path) in enumerate(sample_images.items()):with cols[idx]:if st.button(f"{name}", key=name):selected_sample = file_path# File uploaderst.subheader("Upload a food image:")uploaded_file = st.file_uploader("", type=["jpg", "png", "jpeg"])if selected_sample:# Sample image selectedimage = Image.open(selected_sample)st.image(image, caption="Selected Sample Image", use_container_width=True)classify = st.button("Classify", key="classify_sample")elif uploaded_file is not None:# User uploaded imageimage = Image.open(uploaded_file)st.image(image, caption="Uploaded Image", use_container_width=True)classify = st.button("Classify", key="classify_upload")else:st.info("Please select or upload an image to get started.")classify = Falseif classify:with st.spinner("Classifying..."):# Make predictionspredictions = pipe_classification(image)# Display only the top predictiontop_food = predictions[0]['label']st.header(f"🍽️ Food: {top_food}")# Generate and display ingredients for the top predictionst.subheader("📝 Ingredients")try:ingredients = get_ingredients_qwen(top_food)st.write(ingredients)except Exception as e:st.error(f"Error generating ingredients: {e}")st.subheader("💡 Healthier Alternatives")try:client_gradio = Client("https://8a56cb969da1f9d721.gradio.live/")result = client_gradio.predict(query=f"What's a healthy {top_food} recipe, and why is it healthy?",api_name="/get_response")st.write(result)except Exception as e:st.error(f"Unable to contact RAG: {e}")
app.py+190-67