Vipul Swarup commited on
Commit
b49f46b
·
1 Parent(s): cf2475d

further optimization

Browse files
Files changed (1) hide show
  1. app.py +74 -30
app.py CHANGED
@@ -4,7 +4,6 @@
4
 
5
  import streamlit as st
6
  import json
7
- import markdown
8
 
9
  from langchain.chat_models import ChatOpenAI
10
  from langchain.schema import (
@@ -26,10 +25,10 @@ header_text=str('''
26
  E.g.
27
  ```
28
  {
29
- 'input':'frq-generator',
30
- 'entries':[
31
- {'cc-standard': 'CCSS.ELA-LITERACY.W.4.9', 'area-of-interest': 'Baseball' },
32
- {'cc-standard': 'CCSS.ELA-LITERACY.W.5.8', 'area-of-interest': 'Hiking' }
33
  ]
34
  }
35
  ```
@@ -61,30 +60,74 @@ def load_answer(question):
61
  chatResponseDict = json.loads(assistant_answer.content)
62
 
63
  if (not chatResponseDict['is-math-problem']):
64
- #If a passage is required, generate a 300 word passage, for the appropriate grade level.
65
- if (chatResponseDict['is-passage-required']):
66
- passagePrompt+="Generate a 300 word passage on the given area of interest. It should cover the key points being tested in the common core standard specified. It should draw on real life incidents. Make it about one particular incident that highlights various things about "+question['area-of-interest']+"."
67
- #If external research is required, inform the student what to do research on.
68
- if (chatResponseDict['is-research-required']):
69
- passagePrompt+="Also suggest what specific topic or idea the student can do external research on, to be able to answer questions related to the common core standard specified. Use language as if you are speaking to the student directly, in the formal tone of a teacher or examiner."
70
-
71
- passagePrompt+="Make sure any passage or topics generated are appropriate in terms of vocabulary and comprehensibility for grade "+chatResponseDict['grade-level']
72
- passagePrompt+="Return the response in JSON format."
73
- # Ask the model
74
- st.session_state.sessionMessages.append(HumanMessage(content=passagePrompt))
75
- assistant_answer = chat(st.session_state.sessionMessages)
76
- st.session_state.sessionMessages.append(AIMessage(content=assistant_answer.content))
77
-
78
- # Based on the above generated passage and research topic, we will now generate 3 FRQs and their grading rubrics
79
- frqPrompt='Generate 3 questions based on the previously discussed common core standard, and the above generated passage. The answers should be available either in the above passage, or the student should be able to find them with easy web research. Remember, the student is in grade: '+chatResponseDict['grade-level']
80
- frqPrompt+='Generate a rubric for evaluating the student responses for the above questions. Return the result in JSON format. E.g. { "rubric":[{"question":"..text of the question", "rubric":".. text of the rubric"}] etc.'
81
- frqPrompt+='In the final response JSON, also incldue the passage and research JSONs created above.'
82
- # Ask the model
83
- st.session_state.sessionMessages.append(HumanMessage(content=frqPrompt))
84
- assistant_answer = chat(st.session_state.sessionMessages)
85
- st.session_state.sessionMessages.append(AIMessage(content=assistant_answer.content))
86
-
87
- return json.loads(assistant_answer.content)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
 
89
 
90
  #Gets the user input
@@ -92,7 +135,8 @@ def get_text():
92
  input_text = st.text_area(label="Paste here")
93
  return input_text
94
 
95
- chat = ChatOpenAI(temperature=0, model_name="gpt-4")
 
96
 
97
  user_input = get_text()
98
 
 
4
 
5
  import streamlit as st
6
  import json
 
7
 
8
  from langchain.chat_models import ChatOpenAI
9
  from langchain.schema import (
 
25
  E.g.
26
  ```
27
  {
28
+ "input":"frq-generator",
29
+ "entries":[
30
+ {"cc-standard": "CCSS.ELA-LITERACY.W.4.9", "area-of-interest": "Baseball" },
31
+ {"cc-standard": "CCSS.ELA-LITERACY.W.5.8", "area-of-interest": "Hiking" }
32
  ]
33
  }
34
  ```
 
60
  chatResponseDict = json.loads(assistant_answer.content)
61
 
62
  if (not chatResponseDict['is-math-problem']):
63
+ allChecksPassed=False
64
+ repCount =0;
65
+ while not allChecksPassed and repCount<3:
66
+ #If a passage is required, generate a 300 word passage, for the appropriate grade level.
67
+ if (chatResponseDict['is-passage-required']):
68
+ passagePrompt+="Generate a 300 word passage on the given area of interest. It should cover the key points being tested in the common core standard specified. It should draw on real life incidents. Make it about one particular incident that highlights various things about "+question['area-of-interest']+"."
69
+ #If external research is required, inform the student what to do research on.
70
+ if (chatResponseDict['is-research-required']):
71
+ passagePrompt+="Also suggest what specific topic or idea the student can do external research on, to be able to answer questions related to the common core standard specified. Use language as if you are speaking to the student directly, in the formal tone of a teacher or examiner."
72
+
73
+ passagePrompt+="Make sure any passage or topics generated are appropriate in terms of vocabulary and comprehensibility for grade "+chatResponseDict['grade-level']
74
+ passagePrompt+="Return the response in JSON format."
75
+ # Ask the model
76
+ st.session_state.sessionMessages.append(HumanMessage(content=passagePrompt))
77
+ passage_and_research = chat(st.session_state.sessionMessages)
78
+ st.session_state.sessionMessages.append(AIMessage(content=passage_and_research.content))
79
+
80
+ # Based on the above generated passage and research topic, we will now generate 3 FRQs and their grading rubrics
81
+ frqPrompt='Generate 3 questions based on the previously discussed common core standard, and the above generated passage. The answers should be available either in the above passage, or the student should be able to find them with easy web research. Good questions have: introduction, context, and open-ended question. Remember, the student is in grade: '+chatResponseDict['grade-level']
82
+ frqPrompt+='Generate a rubric for evaluating the student responses for the above questions. Return the result in JSON format. E.g. { "rubric":[{"question":"..text of the question", "rubric":".. text of the rubric"}] etc.'
83
+ frqPrompt+='In the final response JSON, also incldue the passage and research JSONs created above.'
84
+ # Ask the model
85
+ st.session_state.sessionMessages.append(HumanMessage(content=frqPrompt))
86
+ question_and_rubric = chat(st.session_state.sessionMessages)
87
+ st.session_state.sessionMessages.append(AIMessage(content=question_and_rubric.content))
88
+
89
+ ## Do the QC Step
90
+ qcPrompt='For the JSON generated in the previous step, please evaluate for each question and rubric:\
91
+ - Does the FRQ have an introduction, context, and an open-ended question\
92
+ - Does the Rubric prescribe checking for things that can be found in the passage, or suggested research areas.\
93
+ \
94
+ Pass and Fail answers only.\
95
+ Return the response in JSON Form. E.g.:\
96
+ {\
97
+ "type-of-response":"quality control",\
98
+ "qc-for-each-question":[\
99
+ {"FRQ":"Pass", "Rubric":"Fail"}\
100
+ ]\
101
+ }'
102
+ # Ask the model
103
+ st.session_state.sessionMessages.append(HumanMessage(content=qcPrompt))
104
+ qcResponseJson = json.loads(chat(st.session_state.sessionMessages))
105
+
106
+ ## Write the passage & questions, and QC results
107
+ st.write(passage_and_research.content)
108
+ st.write(question_and_rubric.content)
109
+ st.write(qcResponseJson)
110
+
111
+ ## Qc Response doesn't need to into the session state
112
+ # Create a Array of qcResponses, and loop through them
113
+ qcResponseArray = qcResponseJson['qc-for-each-question']
114
+ # If any of the QC checks have failed, we set allChecksPassed to False, and the while loop starts again.
115
+ allChecksPassed=True
116
+ for row in qcResponseArray:
117
+ if (row["FRQ"]=="Fail" or row["Rubric"]=="Fail"):
118
+ allChecksPassed=False
119
+ ## Increase the repCount - we break the While loop after 3 reps, because using GPT4 is expensive, and we don't want it to go into a never ending loop
120
+ repCount+=1
121
+
122
+
123
+
124
+
125
+
126
+
127
+
128
+
129
+ return ""
130
+ # json.loads(assistant_answer.content)
131
 
132
 
133
  #Gets the user input
 
135
  input_text = st.text_area(label="Paste here")
136
  return input_text
137
 
138
+ ## Temperature kept at 0.9, so that generated passages and questions are not too similar to each other.
139
+ chat = ChatOpenAI(temperature=0.9, model_name="gpt-4")
140
 
141
  user_input = get_text()
142