ai_workflows / app /ui /til_feedback.py
theRealNG's picture
restructure the codebase
1f39bb8
import streamlit as st
from dotenv import load_dotenv
from app.workflows.til.analyse_til import TilCrew
from app.workflows.til.analyse_til_v2 import AnalyseTilV2
from streamlit_extras.capture import stdout
from app.workflows.utils.feedback import Feedback
load_dotenv()
def feedback_main():
page_bg_img = '''
<style>
[data-testid="stAppViewContainer"]{
background-image:url("https://www.shutterstock.com/image-vector/abstract-technology-communication-concept-vector-600nw-1914443275.jpg");
background-size:cover;
}
[data-testid="stHeader"]{
background: rgba(0,0,0,0);
}
[data-testid="stToolbar"]{
right: 2rem;
}
</style>
'''
st.markdown(page_bg_img, unsafe_allow_html=True)
st.markdown("<div class='container'>", unsafe_allow_html=True)
st.markdown(
"""
<div class="centered">
<p class="title">Today I Learnt Feedback</p>
<p class="description">Feedback on Today I Learnt</p>
</div>
""",
unsafe_allow_html=True
)
default_content = (
"* Quantization is the process of reducing the size of LLM models by reducing the underlying weights.\n"
"* The weights are reduced by scaling down the datatypes from a datatype that takes larger space to a data type that takes a smaller space, this is also known as downcasting.\n"
"* Quantization offers benefits such as reduced storage space usage and faster computation.\n"
"* Disadvantages: Answers are less precise\n"
"* I learnt how to use Go Routines to handle concurrency in React.\n"
)
if 'til_content' not in st.session_state:
st.session_state.til_content = default_content
til_content = st.text_area(
'Enter what you learnt today:',
value=st.session_state.til_content,
key='til_content',
help='Enter what you learnt today'
)
if st.button("Get Feedback"):
with st.status(
"🤖 **Analysing TIL...**", state="running", expanded=True
) as status:
with st.container(height=500, border=False):
log_container = st.empty()
with stdout(log_container.code, terminator=""):
feedback = AnalyseTilV2()
inputs = {"content": til_content}
results = feedback.kickoff(inputs=inputs)
status.update(
label="✅ Feedback ready!",
state="complete",
expanded=False,
)
st.session_state.results = results
st.session_state.run_id = results["run_id"]
clear_feedback_state(results)
if 'results' in st.session_state:
results = st.session_state.results
for result in results["til"]:
st.markdown(f"#### TIL: {result['takeaway']}")
st.markdown(f"**Feedback:** {result['feedback']}")
if result['feedback'] == "not_ok":
st.markdown(f"**Reason:** {result['reason']}")
if result.get('suggestion') is not None:
st.markdown(f"**Suggestion:** {result['suggestion']}")
feedback_key = result['takeaway'].replace(' ', '_')
feedback_given_key = f"{feedback_key}_feedback_given"
if feedback_given_key not in st.session_state:
st.session_state[feedback_given_key] = False
if not st.session_state[feedback_given_key]:
if st.button("helpful", key=f"helpful_{feedback_key}"):
give_feedback(feedback_key, True)
st.session_state[feedback_given_key] = True
if st.button("not_helpful", key=f"not_helpful_{feedback_key}"):
give_feedback(feedback_key, False)
st.session_state[feedback_given_key] = True
def give_feedback(feedback_key, is_helpful):
run_id = st.session_state.run_id
metric_type = "helpful" if is_helpful else "not_helpful"
metric_score = 1 if is_helpful else 0
feedback_data = Feedback(
metric_type=metric_type,
metric_score=metric_score,
feedback_on=feedback_key.replace('_', ' ').title()
)
try:
TilCrew.post_feedback(run_id, feedback_data)
st.success("Feedback submitted successfully!")
except Exception as e:
st.error(f"Failed to submit feedback: {e}")
st.session_state[f"{feedback_key}_feedback_given"] = True
def clear_feedback_state(results):
for key in st.session_state.keys():
if key.endswith("_feedback_given"):
st.session_state[key] = False
if __name__ == "__main__":
feedback_main()