jblocher commited on
Commit
152e83a
·
1 Parent(s): 1650664

from Eleanor

Browse files
Files changed (1) hide show
  1. app.py +164 -24
app.py CHANGED
@@ -1,42 +1,103 @@
 
 
 
 
 
 
 
 
 
 
1
  import openai
2
- import gradio
 
 
 
3
 
4
- sys_message = """You are a helpful and friendly coach helping a graduate student reflect on their recent class experience in Advanced Corporate Valuation at Vanderbilt's Owen Graduate School of Management.
 
5
  Introduce yourself. Explain that you’re here as their coach to help them reflect on the
6
  experience. Think step by step and wait for the student to answer before doing anything else. Do
7
  not share your plan with students. Reflect on each step of the conversation and then decide what
8
- to do next. Ask only 1 question at a time.
9
- 1. Ask the student to refer back to their reflection at the beginning of the class and during the class.
10
- Then, they should reflect on their class experience, identifying one misconception they had and one new thing they learned about Corporate Valuation.
11
- Wait for a response. Do not proceed until you get a response because you'll need to adapt your next question based on the student's response.
12
  2. Then ask the student: Reflect on these two things.
13
  How has your understanding of [the topics the student mentioned] evolved over the course of the class? If you were to begin a new project now, how would it be different and why?
14
  Do not proceed until you get a response. Do not share your plan with students. Always
15
  wait for a response but do not tell students you are waiting for a response. Ask open-ended
16
  questions but only ask them one at a time. Push students to give you extensive responses
17
- articulating key ideas. They will have seen examples in class, performed a group valuation project,
18
- and just recently turned in their individual valuation project, so any of these could provide experiences for them to reflect on.
19
  Ask follow-up questions. For instance, if a student says they gained a new
20
  understanding of necessary adjustments or calculations ask them to explain their old and new
21
- understanding. Ask them what led to their new insight and/or why these things are important.
22
- These questions prompt a deeper reflection. Push for specific examples from their in-class work, group project, or individual project.
23
- For example, if a student says their view has changed about how to gather and synthesize research,
24
- ask them to provide a concrete example from their in-class work, group project, or individual project.
25
  Specific examples anchor reflections in real learning moments.
26
- Discuss obstacles. Ask the student to consider what obstacles or doubts they still face in valuation.
27
- Discuss strategies for overcoming these obstacles. This helps turn reflections into goal-setting.
28
  Wrap up the conversation by praising reflective thinking. Let the student know when
29
  their reflections are especially thoughtful or demonstrate progress. Let the student know if their
30
  reflections reveal a change or growth in thinking.
31
  """
32
 
33
- des = """
34
- I am your Advanced Corp Val AI Coach. I'm here to help you with your final reflection on this course.
35
- """
36
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
- #model = "gpt-3.5-turbo" # free and fast
39
- #model = "gpt-4" # latest and greatest, not yet available
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
  def CustomChatGPT(message, history):
42
  history_openai_format = [{"role": "system", "content": sys_message}]
@@ -44,9 +105,9 @@ def CustomChatGPT(message, history):
44
  history_openai_format.append({"role": "user", "content": human})
45
  history_openai_format.append({"role": "assistant", "content": assistant})
46
  history_openai_format.append({"role": "user", "content": message})
47
-
48
  response = openai.ChatCompletion.create(
49
- model = "gpt-3.5-turbo",
50
  messages = history_openai_format,
51
  temperature = 1.0,
52
  stream=True
@@ -58,9 +119,88 @@ def CustomChatGPT(message, history):
58
  partial_message = partial_message + chunk['choices'][0]['delta']['content']
59
  yield partial_message
60
 
61
- gradio.ChatInterface(fn=CustomChatGPT,
62
- theme = 'gradio/soft',
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  title = "Advanced Corp Val AI Coach",
64
- description = des).queue().launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
 
 
 
 
 
 
 
 
 
 
 
 
66
 
 
 
 
1
+ # Importing required libraries
2
+
3
+ # openai: used to communicate with the OpenAI API for generating model responses.
4
+ # gradio: provides a simple way to create user interfaces for models.
5
+ # json: allows us to work with JSON data.
6
+ # csv: enables working with CSV files.
7
+ # fpdf: a library to generate PDF files.
8
+
9
+ #! pip install -q openai gradio fpdf
10
+
11
  import openai
12
+ import gradio as gr
13
+ import json
14
+ import csv
15
+ from fpdf import FPDF
16
 
17
+ # Initializing system message which provides initial context and instructions for the model.
18
+ sys_message = """You are a helpful and friendly coach helping a graduate student reflect on their recent class experience in Advanced Corporate Valuation at Vanderbilt's Owen Graduate School of Management.
19
  Introduce yourself. Explain that you’re here as their coach to help them reflect on the
20
  experience. Think step by step and wait for the student to answer before doing anything else. Do
21
  not share your plan with students. Reflect on each step of the conversation and then decide what
22
+ to do next. Ask only 1 question at a time.
23
+ 1. Ask the student to refer back to their reflection at the beginning of the class and during the class.
24
+ Then, they should reflect on their class experience, identifying one misconception they had and one new thing they learned about Corporate Valuation.
25
+ Wait for a response. Do not proceed until you get a response because you'll need to adapt your next question based on the student's response.
26
  2. Then ask the student: Reflect on these two things.
27
  How has your understanding of [the topics the student mentioned] evolved over the course of the class? If you were to begin a new project now, how would it be different and why?
28
  Do not proceed until you get a response. Do not share your plan with students. Always
29
  wait for a response but do not tell students you are waiting for a response. Ask open-ended
30
  questions but only ask them one at a time. Push students to give you extensive responses
31
+ articulating key ideas. They will have seen examples in class, performed a group valuation project,
32
+ and just recently turned in their individual valuation project, so any of these could provide experiences for them to reflect on.
33
  Ask follow-up questions. For instance, if a student says they gained a new
34
  understanding of necessary adjustments or calculations ask them to explain their old and new
35
+ understanding. Ask them what led to their new insight and/or why these things are important.
36
+ These questions prompt a deeper reflection. Push for specific examples from their in-class work, group project, or individual project.
37
+ For example, if a student says their view has changed about how to gather and synthesize research,
38
+ ask them to provide a concrete example from their in-class work, group project, or individual project.
39
  Specific examples anchor reflections in real learning moments.
40
+ Discuss obstacles. Ask the student to consider what obstacles or doubts they still face in valuation.
41
+ Discuss strategies for overcoming these obstacles. This helps turn reflections into goal-setting.
42
  Wrap up the conversation by praising reflective thinking. Let the student know when
43
  their reflections are especially thoughtful or demonstrate progress. Let the student know if their
44
  reflections reveal a change or growth in thinking.
45
  """
46
 
 
 
 
47
 
48
+ # Function Definitions
49
+
50
+ # 1. CustomChatGPT:
51
+ # This function takes a user's message as input, preprocesses the message,
52
+ # interacts with the OpenAI GPT-3 model, and returns the model's response.
53
+ # It manages the conversational context/history and ensures that the
54
+ # conversation flows naturally.
55
+
56
+ # 2. combined_function:
57
+ # This function handles the logic for downloading the chat history in different formats.
58
+ # Based on the user's choice (JSON, CSV, PDF), it calls the appropriate function
59
+ # to convert the chat history and returns a download link.
60
 
61
+ # 3. save_to_json, save_to_csv, save_to_pdf:
62
+ # These helper functions take the chat history as input and save it to the
63
+ # respective file formats.
64
+
65
+ # Let's integrate the extracted function definitions into our documented content.
66
+ def convert_chat_history(format):
67
+ if format == "json":
68
+ file_path = "chat_history.json"
69
+ with open(file_path, 'w') as file:
70
+ json.dump(chat_log, file)
71
+ elif format == "csv":
72
+ file_path = "chat_history.csv"
73
+ with open(file_path, 'w', newline='') as file:
74
+ writer = csv.DictWriter(file, fieldnames=["role", "content"])
75
+ writer.writeheader()
76
+ for row in chat_log:
77
+ writer.writerow(row)
78
+ elif format == "pdf":
79
+ file_path = "chat_history.pdf"
80
+ pdf = FPDF()
81
+ pdf.add_page()
82
+ pdf.set_font("Arial", size=12)
83
+ for entry in chat_log:
84
+ pdf.cell(200, 10, txt="{}: {}".format(entry["role"].title(), entry["content"]), ln=True)
85
+ pdf.output(file_path)
86
+ else:
87
+ return None
88
+ return file_path
89
+
90
+ def download_json():
91
+ convert_chat_history("json")
92
+ return "Downloaded JSON"
93
+
94
+ def download_csv():
95
+ convert_chat_history("csv")
96
+ return "Downloaded CSV"
97
+
98
+ def download_pdf():
99
+ convert_chat_history("pdf")
100
+ return "Downloaded PDF"
101
 
102
  def CustomChatGPT(message, history):
103
  history_openai_format = [{"role": "system", "content": sys_message}]
 
105
  history_openai_format.append({"role": "user", "content": human})
106
  history_openai_format.append({"role": "assistant", "content": assistant})
107
  history_openai_format.append({"role": "user", "content": message})
108
+
109
  response = openai.ChatCompletion.create(
110
+ model = "gpt-3.5-turbo",
111
  messages = history_openai_format,
112
  temperature = 1.0,
113
  stream=True
 
119
  partial_message = partial_message + chunk['choices'][0]['delta']['content']
120
  yield partial_message
121
 
122
+ chat_ui = gr.ChatInterface(fn=CustomChatGPT,
123
+ theme = 'gradio/soft',
124
+ title = "Advanced Corp Val AI Coach",
125
+ description = des)
126
+
127
+ def combined_function(download_format):
128
+ if download_format == "json":
129
+ download_json(history_openai_format)
130
+ return "JSON downloaded", ""
131
+ elif download_format == "csv":
132
+ download_csv(history_openai_format)
133
+ return "CSV downloaded", ""
134
+ elif download_format == "pdf":
135
+ download_pdf(history_openai_format)
136
+ return "PDF downloaded", ""
137
+ else:
138
+ return CustomChatGPT(history_openai_format), ""
139
+
140
+
141
+
142
+ download_ui = gr.Interface(
143
+ fn=combined_function,
144
+ inputs=[gr.Radio(choices = ["json", "csv", "pdf"], label="Download Options")],
145
+ outputs=gr.Textbox(label = "Download status", placeholder = "Downloading..."),
146
+ live=True,
147
+ layout="vertical",
148
+ theme="huggingface",
149
+ server_name="0.0.0.0",
150
+ server_port=7860,
151
+ allow_flagging=False
152
+ )
153
+
154
+ demo = gr.Parallel(chat_ui, download_ui)
155
+ demo.launch()
156
+ # Gradio Interface Creation
157
+
158
+ # Two separate Gradio interfaces are created:
159
+
160
+ # 1. chat_ui:
161
+ # An interface for the chatbot, where users can interact with the AI model
162
+ # in a conversational format. It uses the CustomChatGPT function to process
163
+ # user inputs and generate model responses.
164
+
165
+ # 2. download_ui:
166
+ # An interface that allows users to download the chat history in different
167
+ # formats (JSON, CSV, PDF). The combined_function manages the logic for
168
+ # this interface.
169
+
170
+ # The main execution section of the code initializes these interfaces and
171
+ # launches them in parallel, allowing users to switch between the chatbot
172
+ # and download interfaces.
173
+ chat_ui = gr.ChatInterface(fn=CustomChatGPT,
174
+ theme = 'gradio/soft',
175
  title = "Advanced Corp Val AI Coach",
176
+ description = des)
177
+
178
+ def combined_function(download_format):
179
+ if download_format == "json":
180
+ download_json(history_openai_format)
181
+ return "JSON downloaded", ""
182
+ elif download_format == "csv":
183
+ download_csv(history_openai_format)
184
+ return "CSV downloaded", ""
185
+ elif download_format == "pdf":
186
+ download_pdf(history_openai_format)
187
+ return "PDF downloaded", ""
188
+ else:
189
+ return CustomChatGPT(history_openai_format), ""
190
+
191
+
192
 
193
+ download_ui = gr.Interface(
194
+ fn=combined_function,
195
+ inputs=[gr.Radio(choices = ["json", "csv", "pdf"], label="Download Options")],
196
+ outputs=gr.Textbox(label = "Download status", placeholder = "Downloading..."),
197
+ live=True,
198
+ layout="vertical",
199
+ theme="huggingface",
200
+ server_name="0.0.0.0",
201
+ server_port=7860,
202
+ allow_flagging=False
203
+ )
204
 
205
+ demo = gr.Parallel(chat_ui, download_ui)
206
+ demo.launch()