DKethan commited on
Commit
40109f1
·
verified ·
1 Parent(s): 5a0fc53

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +150 -0
app.py ADDED
@@ -0,0 +1,150 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+ import pandas as pd
4
+ from together import Together
5
+ from utils.helper import *
6
+
7
+
8
+ st.set_page_config(layout="wide")
9
+ st.title("Duel Agent Simulation 🦙🦙")
10
+
11
+
12
+ with st.sidebar:
13
+ with st.expander("Instruction Manual"):
14
+ st.markdown("""
15
+ # 🦙🦙 Duel Agent Simulation Streamlit App
16
+
17
+ ## Overview
18
+
19
+ 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!
20
+
21
+ ## Features
22
+
23
+ ### 📝 Instruction Manual
24
+
25
+ **Meta Llama3 🦙 Chatbot**
26
+
27
+ This application lets you interact with Meta's Llama3 model through a fun interview-style chat.
28
+
29
+ **How to Use:**
30
+ 1. **Input:** Type a topic into the input box labeled "Enter a topic".
31
+ 2. **Submit:** Press the "Submit" button to start the simulation.
32
+ 3. **Chat History:** View the previous conversations as the simulation unfolds.
33
+
34
+ **Credits:**
35
+ - **Developer:** Yiqiao Yin
36
+ - [Site](https://www.y-yin.io/)
37
+ - [LinkedIn](https://www.linkedin.com/in/yiqiaoyin/)
38
+ - [YouTube](https://youtube.com/YiqiaoYin/)
39
+ """)
40
+
41
+ # Text input
42
+ user_topic = st.text_input("Enter a topic", "Data Science")
43
+
44
+ # Add a button to submit
45
+ submit_button = st.button("Run Simulation!")
46
+
47
+ # Add a button to clear the session state
48
+ if st.button("Clear Session"):
49
+ st.session_state.messages = []
50
+ st.experimental_rerun()
51
+
52
+
53
+ # Initialize chat history
54
+ if "messages" not in st.session_state:
55
+ st.session_state.messages = []
56
+
57
+
58
+ # Display chat messages from history on app rerun
59
+ for message in st.session_state.messages:
60
+ with st.chat_message(message["role"]):
61
+ st.markdown(message["content"])
62
+
63
+
64
+ # Create agents
65
+ interviewer = call_llama
66
+ interviewee = call_llama
67
+ judge = call_llama
68
+
69
+
70
+ # React to user input
71
+ iter = 0
72
+ list_of_iters = []
73
+ list_of_questions = []
74
+ list_of_answers = []
75
+ list_of_judge_comments = []
76
+ list_of_passes = []
77
+ if submit_button:
78
+
79
+ # Initiatization
80
+ prompt = f"Ask a question about this topic: {user_topic}"
81
+
82
+ # Display user message in chat message container
83
+ # Default: user=interviewee, assistant=interviewer
84
+ st.chat_message("user").markdown(prompt)
85
+ st.session_state.messages.append({"role": "user", "content": prompt})
86
+
87
+ while True:
88
+
89
+ # Interview asks a question
90
+ question = interviewer(prompt)
91
+
92
+ # Display assistant response in chat message container
93
+ st.chat_message("assistant").markdown(question)
94
+ st.session_state.messages.append({"role": "assistant", "content": question})
95
+
96
+ # Interviewee attempts an answer
97
+ if iter < 5:
98
+ answer = interviewee(
99
+ f"""
100
+ Answer the question: {question} in a mediocre way
101
+ Because you are an inexperienced interviewee.
102
+ """
103
+ )
104
+ st.chat_message("user").markdown(answer)
105
+ st.session_state.messages.append({"role": "user", "content": answer})
106
+ else:
107
+ answer = interviewee(
108
+ f"""
109
+ Answer the question: {question} in a mediocre way
110
+ Because you are an inexperienced interviewee but you really want to learn,
111
+ so you learn from the judge comments: {judge_comments}
112
+ """
113
+ )
114
+ st.chat_message("user").markdown(answer)
115
+ st.session_state.messages.append({"role": "user", "content": answer})
116
+
117
+ # Judge thinks and advises but the thoughts are hidden
118
+ judge_comments = judge(
119
+ f"""
120
+ The question is: {question}
121
+ The answer is: {answer}
122
+ Provide feedback and rate the answer from 1 to 10 while 10 being the best and 1 is the worst.
123
+ """
124
+ )
125
+
126
+
127
+ # Collect all responses
128
+ passed_or_not = 1 if '8' in judge_comments else 0
129
+ list_of_iters.append(iter)
130
+ list_of_questions.append(question)
131
+ list_of_answers.append(answer)
132
+ list_of_judge_comments.append(judge_comments)
133
+ list_of_passes.append(passed_or_not)
134
+ results_tab = pd.DataFrame({
135
+ "Iter.": list_of_iters,
136
+ "Questions": list_of_questions,
137
+ "Answers": list_of_answers,
138
+ "Judge Comments": list_of_judge_comments,
139
+ "Passed": list_of_passes
140
+ })
141
+
142
+ with st.expander("See explanation"):
143
+ st.table(results_tab)
144
+
145
+ # Stopping rule
146
+ if '8' in judge_comments:
147
+ break
148
+
149
+ # Checkpoint
150
+ iter += 1