Spaces:
Build error
Build error
File size: 4,461 Bytes
543a0de 63551f1 543a0de 63551f1 543a0de 63551f1 5864c6f 63551f1 |
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 |
import os
import streamlit as st
import os
import base64
from smolagents import CodeAgent, HfApiModel, FinalAnswerTool
from tools.get_weather import get_weather_forecast
from utils.utils import load_prompt
from dotenv import load_dotenv
from opentelemetry.sdk.trace import TracerProvider
from openinference.instrumentation.smolagents import SmolagentsInstrumentor
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from opentelemetry import trace
if "city" not in st.session_state:
st.session_state.city = ""
if "age" not in st.session_state:
st.session_state.age = ""
if "sex" not in st.session_state:
st.session_state.sex = ""
load_dotenv()
hf_token = os.getenv("HF_TOKEN")
# Set up the OpenTelemetry exporter for LangFuse
LANGFUSE_AUTH = base64.b64encode(
f"{os.environ.get('LANGFUSE_PUBLIC_KEY')}:{os.environ.get('LANGFUSE_SECRET_KEY')}".encode()
).decode()
os.environ["OTEL_EXPORTER_OTLP_ENDPOINT"] = os.environ.get("LANGFUSE_HOST") + "/api/public/otel"
os.environ["OTEL_EXPORTER_OTLP_HEADERS"] = f"Authorization=Basic {LANGFUSE_AUTH}"
# Create a TracerProvider for OpenTelemetry
trace_provider = TracerProvider()
# Add a SimpleSpanProcessor with the OTLPSpanExporter to send traces
trace_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter()))
# Set the global default tracer provider
trace.set_tracer_provider(trace_provider)
tracer = trace.get_tracer(__name__)
# Instrument smolagents with the configured provider
SmolagentsInstrumentor().instrument(tracer_provider=trace_provider)
def get_clothing_reccomendation(city: str, age: str, sex: str) -> str:
"""
This function returns clothing recommendations based on the weather forecast for a specific city.
Args:
city (str): The name of the city for which to get clothing recommendations.
age (str): The age of the user for whom to get clothing recommendations.
Returns:
str: A string containing clothing recommendations based on the weather forecast.
"""
# Load prompt for user message for agent
user_input = load_prompt(
path="prompts/agent_prompt.yaml",
prompt_name="user_message",
city=city,
age=age,
sex=sex
)
# Define LLM model
llm = HfApiModel(token=hf_token)
# Define the list of tools to be used by the agent
tools = [get_weather_forecast, FinalAnswerTool()]
# Initialize the CodeAgent with the Hugging Face model.
agent = CodeAgent(
model=llm,
tools=tools,
max_steps=4,
)
# Run the agent with the user input.
response = agent.run(user_input)
return response
# Streamlit app
st.title("Clothing Recommendation Agent")
st.write(
"Get personalized clothing recommendations based on the weather forecast for your city."
)
st.session_state.city = st.text_input("Enter your city:", st.session_state.city)
st.session_state.age = st.text_input("Enter your age:", st.session_state.age)
st.session_state.sex = st.selectbox("Select your gender", options=["Male", "Female"], index=["Male", "Female"].index(st.session_state.sex) if st.session_state.sex else 0)
# Create a button to get the recommendation
if st.button("Get Recommendation"):
if st.session_state.city and st.session_state.age and st.session_state.sex:
with st.spinner("Fetching outifit suggestions..."):
result = get_clothing_reccomendation(
city=st.session_state.city,
age=st.session_state.age,
sex=st.session_state.sex
)
st.success("Here are your outfit sugegestions:")
# Display the results
try:
for idx, outfit in enumerate(result):
with st.expander(f"π Style #{idx + 1}: {outfit['style']}", expanded=True):
st.markdown(f"**π Top:** {outfit['top']}")
st.markdown(f"**π Bottom:** {outfit['bottom']}")
st.markdown(f"**π Shoes:** {outfit['shoes']}")
st.markdown(f"**π§’ Accessories:** {outfit['accessories']}")
except Exception as e:
st.write(result)
else:
st.error("Please fill in all fields.")
# Reset button to clear the session state
if st.button("Reset"):
st.session_state.city = ""
st.session_state.age = ""
st.session_state.sex = ""
st.rerun() |