AliInamdar commited on
Commit
160a98a
·
verified ·
1 Parent(s): 3e706fe

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +148 -0
app.py ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 ChatBot 🤖")
10
+
11
+
12
+ with st.sidebar:
13
+ with st.expander("Instruction Manual"):
14
+ st.markdown("""
15
+ # Duel Agent ChatBot Streamlit App
16
+
17
+ ## Overview
18
+
19
+ Welcome to the **Duel Agent ChatBot** 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:** Ali Inamdar
36
+
37
+ """)
38
+
39
+ # Text input
40
+ user_topic = st.text_input("Enter a topic", "Data Science")
41
+
42
+ # Add a button to submit
43
+ submit_button = st.button("Run Simulation!")
44
+
45
+ # Add a button to clear the session state
46
+ if st.button("Clear Session"):
47
+ st.session_state.messages = []
48
+ st.experimental_rerun()
49
+
50
+
51
+ # Initialize chat history
52
+ if "messages" not in st.session_state:
53
+ st.session_state.messages = []
54
+
55
+
56
+ # Display chat messages from history on app rerun
57
+ for message in st.session_state.messages:
58
+ with st.chat_message(message["role"]):
59
+ st.markdown(message["content"])
60
+
61
+
62
+ # Create agents
63
+ interviewer = call_llama
64
+ interviewee = call_llama
65
+ judge = call_llama
66
+
67
+
68
+ # React to user input
69
+ iter = 0
70
+ list_of_iters = []
71
+ list_of_questions = []
72
+ list_of_answers = []
73
+ list_of_judge_comments = []
74
+ list_of_passes = []
75
+ if submit_button:
76
+
77
+ # Initiatization
78
+ prompt = f"Ask a question about this topic: {user_topic}"
79
+
80
+ # Display user message in chat message container
81
+ # Default: user=interviewee, assistant=interviewer
82
+ st.chat_message("user").markdown(prompt)
83
+ st.session_state.messages.append({"role": "user", "content": prompt})
84
+
85
+ while True:
86
+
87
+ # Interview asks a question
88
+ question = interviewer(prompt)
89
+
90
+ # Display assistant response in chat message container
91
+ st.chat_message("assistant").markdown(question)
92
+ st.session_state.messages.append({"role": "assistant", "content": question})
93
+
94
+ # Interviewee attempts an answer
95
+ if iter < 5:
96
+ answer = interviewee(
97
+ f"""
98
+ Answer the question: {question} in a mediocre way
99
+ Because you are an inexperienced interviewee.
100
+ """
101
+ )
102
+ st.chat_message("user").markdown(answer)
103
+ st.session_state.messages.append({"role": "user", "content": answer})
104
+ else:
105
+ answer = interviewee(
106
+ f"""
107
+ Answer the question: {question} in a mediocre way
108
+ Because you are an inexperienced interviewee but you really want to learn,
109
+ so you learn from the judge comments: {judge_comments}
110
+ """
111
+ )
112
+ st.chat_message("user").markdown(answer)
113
+ st.session_state.messages.append({"role": "user", "content": answer})
114
+
115
+ # Judge thinks and advises but the thoughts are hidden
116
+ judge_comments = judge(
117
+ f"""
118
+ The question is: {question}
119
+ The answer is: {answer}
120
+ Provide feedback and rate the answer from 1 to 10 while 10 being the best and 1 is the worst.
121
+ """
122
+ )
123
+
124
+
125
+ # Collect all responses
126
+ passed_or_not = 1 if '8' in judge_comments else 0
127
+ list_of_iters.append(iter)
128
+ list_of_questions.append(question)
129
+ list_of_answers.append(answer)
130
+ list_of_judge_comments.append(judge_comments)
131
+ list_of_passes.append(passed_or_not)
132
+ results_tab = pd.DataFrame({
133
+ "Iter.": list_of_iters,
134
+ "Questions": list_of_questions,
135
+ "Answers": list_of_answers,
136
+ "Judge Comments": list_of_judge_comments,
137
+ "Passed": list_of_passes
138
+ })
139
+
140
+ with st.expander("See explanation"):
141
+ st.table(results_tab)
142
+
143
+ # Stopping rule
144
+ if '8' in judge_comments:
145
+ break
146
+
147
+ # Checkpoint
148
+ iter += 1