Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,8 +2,7 @@ import asyncio
|
|
| 2 |
import streamlit as st
|
| 3 |
import os
|
| 4 |
import pandas as pd
|
| 5 |
-
from
|
| 6 |
-
from helper import *
|
| 7 |
|
| 8 |
st.set_page_config(layout="wide")
|
| 9 |
st.title("Duel Agent Simulation π¦π¦")
|
|
@@ -12,24 +11,16 @@ with st.sidebar:
|
|
| 12 |
with st.expander("Instruction Manual"):
|
| 13 |
st.markdown("""
|
| 14 |
# π¦π¦ Duel Agent Simulation Streamlit App
|
| 15 |
-
|
| 16 |
## Overview
|
| 17 |
-
|
| 18 |
Welcome to the **Duel Agent Simulation** app! This Streamlit application allows you to chat with Meta's Llama3 model in a unique interview simulation. The app features two agents in an interview scenario, with a judge providing feedback. The best part? You simply provide a topic, and the simulation runs itself!
|
| 19 |
-
|
| 20 |
## Features
|
| 21 |
-
|
| 22 |
### π Instruction Manual
|
| 23 |
-
|
| 24 |
**Meta Llama3 π¦ Chatbot**
|
| 25 |
-
|
| 26 |
This application lets you interact with Meta's Llama3 model through a fun interview-style chat.
|
| 27 |
-
|
| 28 |
**How to Use:**
|
| 29 |
1. **Input:** Type a topic into the input box labeled "Enter a topic".
|
| 30 |
-
2. **Submit:** Press the "
|
| 31 |
3. **Chat History:** View the previous conversations as the simulation unfolds.
|
| 32 |
-
|
| 33 |
**Credits:**
|
| 34 |
- **Developer:** Kethan Dosapati
|
| 35 |
- [LinkedIn](https://www.linkedin.com/in/kethan-dosapati/)
|
|
@@ -44,11 +35,22 @@ with st.sidebar:
|
|
| 44 |
# Add a button to clear the session state
|
| 45 |
if st.button("Clear Session"):
|
| 46 |
st.session_state.messages = []
|
|
|
|
| 47 |
st.experimental_rerun()
|
| 48 |
|
| 49 |
# Initialize chat history
|
| 50 |
if "messages" not in st.session_state:
|
| 51 |
st.session_state.messages = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
# Display chat messages from history on app rerun
|
| 54 |
for message in st.session_state.messages:
|
|
@@ -61,82 +63,68 @@ interviewee = call_llama
|
|
| 61 |
judge = call_llama
|
| 62 |
|
| 63 |
# React to user input
|
| 64 |
-
iter = 0
|
| 65 |
-
list_of_iters = []
|
| 66 |
-
list_of_questions = []
|
| 67 |
-
list_of_answers = []
|
| 68 |
-
list_of_judge_comments = []
|
| 69 |
-
list_of_passes = []
|
| 70 |
if submit_button:
|
| 71 |
-
|
| 72 |
-
# Initiatization
|
| 73 |
prompt = f"Ask a question about this topic: {user_topic}"
|
| 74 |
|
| 75 |
# Display user message in chat message container
|
| 76 |
-
|
| 77 |
-
st.
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
)
|
| 97 |
-
st.chat_message("user").markdown(answer)
|
| 98 |
-
st.session_state.messages.append({"role": "user", "content": answer})
|
| 99 |
-
else:
|
| 100 |
-
answer = interviewee(
|
| 101 |
-
f"""
|
| 102 |
-
Answer the question: {question} in a mediocre way
|
| 103 |
-
Because you are an inexperienced interviewee but you really want to learn,
|
| 104 |
-
so you learn from the judge comments: {judge_comments}
|
| 105 |
-
"""
|
| 106 |
-
)
|
| 107 |
st.chat_message("user").markdown(answer)
|
| 108 |
st.session_state.messages.append({"role": "user", "content": answer})
|
| 109 |
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
import os
|
| 4 |
import pandas as pd
|
| 5 |
+
from helper import call_llama # Make sure this is properly defined in helper.py
|
|
|
|
| 6 |
|
| 7 |
st.set_page_config(layout="wide")
|
| 8 |
st.title("Duel Agent Simulation π¦π¦")
|
|
|
|
| 11 |
with st.expander("Instruction Manual"):
|
| 12 |
st.markdown("""
|
| 13 |
# π¦π¦ Duel Agent Simulation Streamlit App
|
|
|
|
| 14 |
## Overview
|
|
|
|
| 15 |
Welcome to the **Duel Agent Simulation** app! This Streamlit application allows you to chat with Meta's Llama3 model in a unique interview simulation. The app features two agents in an interview scenario, with a judge providing feedback. The best part? You simply provide a topic, and the simulation runs itself!
|
|
|
|
| 16 |
## Features
|
|
|
|
| 17 |
### π Instruction Manual
|
|
|
|
| 18 |
**Meta Llama3 π¦ Chatbot**
|
|
|
|
| 19 |
This application lets you interact with Meta's Llama3 model through a fun interview-style chat.
|
|
|
|
| 20 |
**How to Use:**
|
| 21 |
1. **Input:** Type a topic into the input box labeled "Enter a topic".
|
| 22 |
+
2. **Submit:** Press the "Run Simulation!" button to start the simulation.
|
| 23 |
3. **Chat History:** View the previous conversations as the simulation unfolds.
|
|
|
|
| 24 |
**Credits:**
|
| 25 |
- **Developer:** Kethan Dosapati
|
| 26 |
- [LinkedIn](https://www.linkedin.com/in/kethan-dosapati/)
|
|
|
|
| 35 |
# Add a button to clear the session state
|
| 36 |
if st.button("Clear Session"):
|
| 37 |
st.session_state.messages = []
|
| 38 |
+
st.session_state.simulation_data = []
|
| 39 |
st.experimental_rerun()
|
| 40 |
|
| 41 |
# Initialize chat history
|
| 42 |
if "messages" not in st.session_state:
|
| 43 |
st.session_state.messages = []
|
| 44 |
+
|
| 45 |
+
# Initialize simulation data storage
|
| 46 |
+
if "simulation_data" not in st.session_state:
|
| 47 |
+
st.session_state.simulation_data = {
|
| 48 |
+
"iterations": [],
|
| 49 |
+
"questions": [],
|
| 50 |
+
"answers": [],
|
| 51 |
+
"judge_comments": [],
|
| 52 |
+
"passes": []
|
| 53 |
+
}
|
| 54 |
|
| 55 |
# Display chat messages from history on app rerun
|
| 56 |
for message in st.session_state.messages:
|
|
|
|
| 63 |
judge = call_llama
|
| 64 |
|
| 65 |
# React to user input
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
if submit_button:
|
| 67 |
+
iter = 0
|
|
|
|
| 68 |
prompt = f"Ask a question about this topic: {user_topic}"
|
| 69 |
|
| 70 |
# Display user message in chat message container
|
| 71 |
+
st.chat_message("user").markdown(f"Topic: {user_topic}")
|
| 72 |
+
st.session_state.messages.append({"role": "user", "content": f"Topic: {user_topic}"})
|
| 73 |
+
|
| 74 |
+
with st.spinner("Running simulation..."):
|
| 75 |
+
while True:
|
| 76 |
+
# Interviewer asks a question
|
| 77 |
+
question = interviewer(prompt)
|
| 78 |
+
|
| 79 |
+
# Display question
|
| 80 |
+
st.chat_message("assistant").markdown(question)
|
| 81 |
+
st.session_state.messages.append({"role": "assistant", "content": question})
|
| 82 |
+
|
| 83 |
+
# Interviewee answers
|
| 84 |
+
if iter < 2: # First few answers are mediocre
|
| 85 |
+
answer_prompt = f"Answer the question: {question} in a mediocre way because you are an inexperienced interviewee."
|
| 86 |
+
else: # Later answers try to improve
|
| 87 |
+
judge_comments = st.session_state.simulation_data["judge_comments"][-1] if iter > 0 else ""
|
| 88 |
+
answer_prompt = f"""Answer the question: {question} in a mediocre way because you are an inexperienced interviewee,
|
| 89 |
+
but you're learning from these judge comments: {judge_comments}"""
|
| 90 |
+
|
| 91 |
+
answer = interviewee(answer_prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
st.chat_message("user").markdown(answer)
|
| 93 |
st.session_state.messages.append({"role": "user", "content": answer})
|
| 94 |
|
| 95 |
+
# Judge evaluates
|
| 96 |
+
judge_prompt = f"""The question was: {question}
|
| 97 |
+
The answer was: {answer}
|
| 98 |
+
Provide brief feedback and rate the answer from 1 to 10 (10 being best)."""
|
| 99 |
+
judge_comments = judge(judge_prompt)
|
| 100 |
+
|
| 101 |
+
# Store results
|
| 102 |
+
passed = 1 if any(str(i) in judge_comments for i in [8, 9, 10]) else 0
|
| 103 |
+
st.session_state.simulation_data["iterations"].append(iter)
|
| 104 |
+
st.session_state.simulation_data["questions"].append(question)
|
| 105 |
+
st.session_state.simulation_data["answers"].append(answer)
|
| 106 |
+
st.session_state.simulation_data["judge_comments"].append(judge_comments)
|
| 107 |
+
st.session_state.simulation_data["passes"].append(passed)
|
| 108 |
+
|
| 109 |
+
# Show results table
|
| 110 |
+
with st.expander("Simulation Details"):
|
| 111 |
+
results_df = pd.DataFrame({
|
| 112 |
+
"Iteration": st.session_state.simulation_data["iterations"],
|
| 113 |
+
"Question": st.session_state.simulation_data["questions"],
|
| 114 |
+
"Answer": st.session_state.simulation_data["answers"],
|
| 115 |
+
"Judge Feedback": st.session_state.simulation_data["judge_comments"],
|
| 116 |
+
"Passed?": ["β
" if x else "β" for x in st.session_state.simulation_data["passes"]]
|
| 117 |
+
})
|
| 118 |
+
st.table(results_df)
|
| 119 |
+
|
| 120 |
+
# Stopping condition
|
| 121 |
+
if passed:
|
| 122 |
+
st.success("Interview passed! The candidate gave a good enough answer.")
|
| 123 |
+
break
|
| 124 |
+
|
| 125 |
+
if iter >= 5: # Max iterations
|
| 126 |
+
st.warning("Interview ended - maximum attempts reached")
|
| 127 |
+
break
|
| 128 |
+
|
| 129 |
+
iter += 1
|
| 130 |
+
prompt = f"Ask another follow-up question about: {user_topic}"
|