Spaces:
Sleeping
Sleeping
| import json | |
| import uuid | |
| import requests | |
| import streamlit as st | |
| # Set up page configuration | |
| st.set_page_config( | |
| page_title="SlimFit Coach", | |
| layout="wide", | |
| page_icon="🌊", | |
| ) | |
| # Generate or load existing session ID | |
| if "id" not in st.session_state: | |
| st.session_state.id = str(uuid.uuid4()).lower() | |
| # Title of the app | |
| st.title("SlimFit Coach") | |
| # Sidebar customization options | |
| st.sidebar.title("SlimFit Coach Settings") | |
| # Default Messages Section | |
| with st.sidebar.expander("Default Messages", expanded=True): | |
| welcome_message = st.text_area( | |
| "Welcome Message", | |
| value="Hello! I’m SlimFit Coach, your personal weight loss coach. How can I assist you on your journey to achieving your weight loss goals?", | |
| height=100, | |
| ) | |
| # Default Question Section | |
| with st.sidebar.expander("Default Question", expanded=True): | |
| if st.button("Add Question"): | |
| st.session_state.default_question = st.text_area( | |
| "Question", | |
| value='''- "Objective: What are the main weight loss goals you're aiming to achieve? (For example, specific weight loss target, fitness improvement, etc.?)"''', | |
| height=200, | |
| ) | |
| # Coaching Style Section | |
| with st.sidebar.expander("Coaching Style", expanded=True): | |
| # Function to create dual-label sliders with total 100 | |
| def dual_slider(label_left, label_right, default_val): | |
| slider_val = st.slider(f"{label_left} ↔ {label_right}", 0, 100, default_val) | |
| return slider_val, 100 - slider_val | |
| # Sliders for promptValues parameters | |
| question_val, advice_val = dual_slider("Question", "Advice", 50) | |
| gentle_val, direct_val = dual_slider("Gentle", "Direct", 50) | |
| short_val, long_val = dual_slider("Short", "Long", 50) | |
| accepting_val, challenging_val = dual_slider("Accepting", "Challenging", 50) | |
| serious_val, light_hearted_val = dual_slider("Serious", "Light hearted", 50) | |
| # Display the calculated values for verification (Optional) | |
| st.markdown(f"**Question**: {question_val}, **Advice**: {advice_val}") | |
| st.markdown(f"**Gentle**: {gentle_val}, **Direct**: {direct_val}") | |
| st.markdown(f"**Short**: {short_val}, **Long**: {long_val}") | |
| st.markdown(f"**Accepting**: {accepting_val}, **Challenging**: {challenging_val}") | |
| st.markdown(f"**Serious**: {serious_val}, **Light hearted**: {light_hearted_val}") | |
| # Main chat-like interface | |
| API_URL = "https://startrz-ai-agent.hf.space/api/v1/prediction/05225105-b77e-4366-b536-662139dc1e98" | |
| # Function to query the new API | |
| def query(payload): | |
| try: | |
| response = requests.post(API_URL, json=payload) | |
| response.raise_for_status() # Raise an exception for HTTP errors | |
| return response.json() | |
| except requests.exceptions.RequestException as e: | |
| return {"error": str(e)} | |
| # Initialize session state for messages if not already set | |
| if "messages" not in st.session_state: | |
| st.session_state.messages = [] | |
| # Display previous messages | |
| for message in st.session_state.messages: | |
| with st.chat_message(message["role"]): | |
| st.markdown(message["content"]) | |
| # Handle new user input | |
| if prompt := st.chat_input("Message SlimFit Coach"): | |
| # Append user message to session state | |
| st.session_state.messages.append({"role": "user", "content": prompt}) | |
| with st.chat_message("user"): | |
| st.markdown(prompt) | |
| # Placeholder for AI response | |
| with st.spinner(text='Querying SlimFit Coach...'): | |
| with st.chat_message("assistant"): | |
| message_placeholder = st.empty() | |
| payload = { | |
| "question": prompt, | |
| "overrideConfig": { | |
| "sessionId": st.session_state.id, | |
| "promptValues": { | |
| "text": "{{question}}", | |
| "question": question_val, | |
| "advice": advice_val, | |
| "gentle": gentle_val, | |
| "direct": direct_val, | |
| "short": short_val, | |
| "long": long_val, | |
| "accepting": accepting_val, | |
| "challenging": challenging_val, | |
| "serious": serious_val, | |
| "light_hearted": light_hearted_val, | |
| "welcome_message": welcome_message, | |
| "default_question": st.session_state.default_question, | |
| } | |
| } | |
| } | |
| # Generate response from the new API | |
| response = query(payload) | |
| # Parse the JSON response | |
| text = response.get("text", "Sorry, I couldn't understand the response.") | |
| # Display the AI response | |
| message_placeholder.markdown(text) | |
| # Append AI message to session state | |
| st.session_state.messages.append({"role": "assistant", "content": text}) |