agentic update
Browse files
app.py
CHANGED
|
@@ -4,18 +4,13 @@ import streamlit as st
|
|
| 4 |
from openai import OpenAI
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
|
| 7 |
-
# For OCR
|
| 8 |
-
import pytesseract
|
| 9 |
-
from PIL import Image
|
| 10 |
-
|
| 11 |
-
# Set page to wide layout so you can see everything
|
| 12 |
-
st.set_page_config(layout="wide")
|
| 13 |
-
|
| 14 |
load_dotenv()
|
| 15 |
-
|
| 16 |
-
# Initialize OpenAI client
|
| 17 |
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
def default_prompt_builder(
|
| 20 |
math_subject: str,
|
| 21 |
topic: str,
|
|
@@ -27,7 +22,7 @@ def default_prompt_builder(
|
|
| 27 |
prev_lecture: str
|
| 28 |
) -> str:
|
| 29 |
"""
|
| 30 |
-
Builds the
|
| 31 |
"""
|
| 32 |
return f"""
|
| 33 |
You are a math teacher. Create a comprehensive lecture script for a lesson in "{math_subject}" on the topic "{topic}"
|
|
@@ -44,44 +39,33 @@ The script must be fluent with smooth transitions and detailed explanations of t
|
|
| 44 |
2. **Topic Intro :**
|
| 45 |
Present a concise background or interesting fact about the topic.
|
| 46 |
|
| 47 |
-
3. **Core Concept Definition:**
|
| 48 |
Provide a clear definition of the core concepts. Explain their importance and mention any
|
| 49 |
fundamental equations, theorems, or limit definitions that underlie them. Include mathematical equations if necessary.
|
| 50 |
|
| 51 |
-
4. **Detailed Explanation:**
|
| 52 |
Offer a detailed, step-by-step explanation of the topic. Incorporate deeper theoretical underpinnings—
|
| 53 |
such as derivations from first principles or explanations of common misconceptions and how to avoid them.
|
| 54 |
|
| 55 |
-
5. **Example Problem:**
|
| 56 |
-
Use the following example problem for a thorough walkthrough
|
| 57 |
"{example1}"
|
| 58 |
-
- Provide a
|
| 59 |
-
- Write out the mathematical equations.
|
| 60 |
- Reference relevant theorems or limit definitions.
|
| 61 |
-
-
|
| 62 |
-
-
|
| 63 |
-
- Clearly summarize the final result.
|
| 64 |
|
| 65 |
-
6. **Problem 1:**
|
| 66 |
Use the following second problem:
|
| 67 |
"{problem1}"
|
| 68 |
-
Provide a
|
| 69 |
-
- Step-by-step solution with numbered reasoning. Name it as step 1, step 2, etc.
|
| 70 |
-
- Reference relevant theorems/definitions.
|
| 71 |
-
- Write out the mathematical equations.
|
| 72 |
-
- Summarize the final result.
|
| 73 |
|
| 74 |
-
7. **Word Problem:**
|
| 75 |
Use the following word problem:
|
| 76 |
"{wordproblem}"
|
| 77 |
-
Solve it thoroughly and
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
- Write out the mathematical equations.
|
| 81 |
-
- Using transitions, naturally add common pitfalls and offer alternative approaches.
|
| 82 |
-
- Provide a clear final summary.
|
| 83 |
-
|
| 84 |
-
8. **Engagement, Reinforcement, and Conclusion:**
|
| 85 |
Summarize the key points and offer additional tips or alternative approaches for deeper understanding.
|
| 86 |
End with a motivational wrap-up, leaving the audience with a final thought or question.
|
| 87 |
|
|
@@ -89,7 +73,9 @@ Begin your response now.
|
|
| 89 |
"""
|
| 90 |
|
| 91 |
def call_openai_chat(prompt: str, max_tokens: int = 2000, temperature: float = 0.4) -> str:
|
| 92 |
-
"""
|
|
|
|
|
|
|
| 93 |
response = client.chat.completions.create(
|
| 94 |
model="gpt-4o-2024-08-06",
|
| 95 |
messages=[{"role": "user", "content": prompt}],
|
|
@@ -98,159 +84,209 @@ def call_openai_chat(prompt: str, max_tokens: int = 2000, temperature: float = 0
|
|
| 98 |
)
|
| 99 |
return response.choices[0].message.content.strip()
|
| 100 |
|
| 101 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
"""
|
| 103 |
-
|
| 104 |
-
|
| 105 |
"""
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
"""
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
or
|
| 121 |
"""
|
| 122 |
prompt = f"""
|
| 123 |
-
You
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
-
|
| 126 |
|
| 127 |
-
|
| 128 |
-
"{
|
| 129 |
-
|
| 130 |
-
Please provide a helpful rewrite that addresses the user's request while preserving all essential details.
|
| 131 |
"""
|
| 132 |
-
|
| 133 |
-
model="gpt-4o-2024-08-06",
|
| 134 |
-
messages=[
|
| 135 |
-
{"role": "system", "content": "You are an expert academic editor specialized in lecture scripts."},
|
| 136 |
-
{"role": "user", "content": prompt}
|
| 137 |
-
],
|
| 138 |
-
max_tokens=max_tokens,
|
| 139 |
-
temperature=temperature
|
| 140 |
-
)
|
| 141 |
-
return response.choices[0].message.content.strip()
|
| 142 |
|
| 143 |
-
#
|
|
|
|
|
|
|
| 144 |
|
| 145 |
-
st.
|
| 146 |
|
| 147 |
-
|
| 148 |
-
st.sidebar.
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
|
|
|
| 154 |
|
| 155 |
-
#
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
example_text_input = st.sidebar.text_area(
|
| 159 |
-
"Or type Example Problem text",
|
| 160 |
-
"Given the graph with the points A(2,7), B(-1,5), C(-2,-3), and D(5,-8), determine the domain and range of the graph"
|
| 161 |
-
)
|
| 162 |
|
| 163 |
-
|
| 164 |
-
image = Image.open(uploaded_example_image)
|
| 165 |
-
extracted_text = pytesseract.image_to_string(image)
|
| 166 |
-
st.sidebar.write("**Extracted text from Example Problem image:**", extracted_text)
|
| 167 |
-
example1 = extracted_text.strip() if extracted_text.strip() else example_text_input
|
| 168 |
-
else:
|
| 169 |
-
example1 = example_text_input
|
| 170 |
-
|
| 171 |
-
# --- OCR + Text for Problem 1 ---
|
| 172 |
-
st.sidebar.header("Problem 1")
|
| 173 |
-
uploaded_problem1_image = st.sidebar.file_uploader("Upload Problem 1 Image (optional)", type=["png", "jpg", "jpeg"])
|
| 174 |
-
problem1_text_input = st.sidebar.text_area(
|
| 175 |
-
"Or type Problem 1 text",
|
| 176 |
-
"Solve the equation when the domain is {-2, -1, 0, 4, 7} for 6x - 3y = 21"
|
| 177 |
-
)
|
| 178 |
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
st.sidebar.write("**Extracted text from Problem 1 image:**", extracted_text)
|
| 183 |
-
problem1 = extracted_text.strip() if extracted_text.strip() else problem1_text_input
|
| 184 |
-
else:
|
| 185 |
-
problem1 = problem1_text_input
|
| 186 |
-
|
| 187 |
-
# --- OCR + Text for Word Problem ---
|
| 188 |
-
st.sidebar.header("Word Problem")
|
| 189 |
-
uploaded_wordproblem_image = st.sidebar.file_uploader("Upload Word Problem Image (optional)", type=["png", "jpg", "jpeg"])
|
| 190 |
-
wordproblem_text_input = st.sidebar.text_area(
|
| 191 |
-
"Or type Word Problem text",
|
| 192 |
-
"On a Friday night, a group of friends decided to go to the movies..."
|
| 193 |
)
|
| 194 |
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
st.sidebar.write("**Extracted text from Word Problem image:**", extracted_text)
|
| 199 |
-
wordproblem = extracted_text.strip() if extracted_text.strip() else wordproblem_text_input
|
| 200 |
-
else:
|
| 201 |
-
wordproblem = wordproblem_text_input
|
| 202 |
-
|
| 203 |
-
# --- Prompt Customization ---
|
| 204 |
-
st.sidebar.header("Prompt Customization")
|
| 205 |
-
edit_prompt_manually = st.sidebar.checkbox("Edit Prompt Manually?")
|
| 206 |
-
|
| 207 |
-
if edit_prompt_manually:
|
| 208 |
-
# Build the default prompt for the user to see and edit
|
| 209 |
-
default_prompt = default_prompt_builder(
|
| 210 |
-
math_subject, topic, difficulty, speaking_style,
|
| 211 |
-
example1, problem1, wordproblem, prev_lecture
|
| 212 |
-
)
|
| 213 |
-
user_custom_prompt = st.sidebar.text_area(
|
| 214 |
-
"Custom Prompt (edit as you like)",
|
| 215 |
-
value=default_prompt,
|
| 216 |
-
height=600
|
| 217 |
-
)
|
| 218 |
-
else:
|
| 219 |
-
user_custom_prompt = None
|
| 220 |
-
|
| 221 |
-
# 1) Build the final prompt right away
|
| 222 |
-
if edit_prompt_manually and user_custom_prompt:
|
| 223 |
-
final_prompt = user_custom_prompt
|
| 224 |
-
else:
|
| 225 |
-
final_prompt = default_prompt_builder(
|
| 226 |
-
math_subject, topic, difficulty, speaking_style,
|
| 227 |
-
example1, problem1, wordproblem, prev_lecture
|
| 228 |
-
)
|
| 229 |
|
| 230 |
-
|
| 231 |
-
st.
|
| 232 |
-
st.
|
|
|
|
| 233 |
|
| 234 |
-
#
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
lecture_script = call_openai_chat(final_prompt, max_tokens=3000, temperature=0.4)
|
| 238 |
-
lecture_script = unify_all_math_as_double_dollars(lecture_script)
|
| 239 |
-
|
| 240 |
-
st.success("Lecture script generated!")
|
| 241 |
-
st.text_area("Final Lecture Script", lecture_script, height=600)
|
| 242 |
-
st.session_state["final_script"] = lecture_script # store for agent use
|
| 243 |
-
|
| 244 |
-
# --- Agent Interaction Section ---
|
| 245 |
-
st.subheader("Request Further Improvements or Checks")
|
| 246 |
-
user_improvement_request = st.text_input(
|
| 247 |
-
"Request (e.g., 'Add better transitions', 'Improve step-by-step explanation', etc.)"
|
| 248 |
-
)
|
| 249 |
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from openai import OpenAI
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
load_dotenv()
|
|
|
|
|
|
|
| 8 |
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 9 |
|
| 10 |
+
# --------------------------------------------
|
| 11 |
+
# Core Functions
|
| 12 |
+
# --------------------------------------------
|
| 13 |
+
|
| 14 |
def default_prompt_builder(
|
| 15 |
math_subject: str,
|
| 16 |
topic: str,
|
|
|
|
| 22 |
prev_lecture: str
|
| 23 |
) -> str:
|
| 24 |
"""
|
| 25 |
+
Builds the base lecture prompt.
|
| 26 |
"""
|
| 27 |
return f"""
|
| 28 |
You are a math teacher. Create a comprehensive lecture script for a lesson in "{math_subject}" on the topic "{topic}"
|
|
|
|
| 39 |
2. **Topic Intro :**
|
| 40 |
Present a concise background or interesting fact about the topic.
|
| 41 |
|
| 42 |
+
3. **Core Concept Definition :**
|
| 43 |
Provide a clear definition of the core concepts. Explain their importance and mention any
|
| 44 |
fundamental equations, theorems, or limit definitions that underlie them. Include mathematical equations if necessary.
|
| 45 |
|
| 46 |
+
4. **Detailed Explanation :**
|
| 47 |
Offer a detailed, step-by-step explanation of the topic. Incorporate deeper theoretical underpinnings—
|
| 48 |
such as derivations from first principles or explanations of common misconceptions and how to avoid them.
|
| 49 |
|
| 50 |
+
5. **Example Problem :**
|
| 51 |
+
Use the following example problem for a thorough walkthrough:
|
| 52 |
"{example1}"
|
| 53 |
+
- Provide a step-by-step solution (e.g., Step1, Step2, Step3, etc.).
|
|
|
|
| 54 |
- Reference relevant theorems or limit definitions.
|
| 55 |
+
- Mention common mistakes with transitions and how to correct them.
|
| 56 |
+
- Summarize the final result clearly.
|
|
|
|
| 57 |
|
| 58 |
+
6. **Problem 1 :**
|
| 59 |
Use the following second problem:
|
| 60 |
"{problem1}"
|
| 61 |
+
Provide a detailed solution with numbered steps and transitions, referencing theorems/definitions, and summarizing the result.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
7. **Word Problem :**
|
| 64 |
Use the following word problem:
|
| 65 |
"{wordproblem}"
|
| 66 |
+
Solve it thoroughly with numbered steps and transitions, referencing key theorems, discussing pitfalls, and offering alternative approaches.
|
| 67 |
+
|
| 68 |
+
8. **Engagement, Reinforcement, and Conclusion :**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
Summarize the key points and offer additional tips or alternative approaches for deeper understanding.
|
| 70 |
End with a motivational wrap-up, leaving the audience with a final thought or question.
|
| 71 |
|
|
|
|
| 73 |
"""
|
| 74 |
|
| 75 |
def call_openai_chat(prompt: str, max_tokens: int = 2000, temperature: float = 0.4) -> str:
|
| 76 |
+
"""
|
| 77 |
+
Calls the OpenAI chat completions API with the provided prompt.
|
| 78 |
+
"""
|
| 79 |
response = client.chat.completions.create(
|
| 80 |
model="gpt-4o-2024-08-06",
|
| 81 |
messages=[{"role": "user", "content": prompt}],
|
|
|
|
| 84 |
)
|
| 85 |
return response.choices[0].message.content.strip()
|
| 86 |
|
| 87 |
+
def check_transitions(lecture_script: str) -> str:
|
| 88 |
+
prompt = f"""
|
| 89 |
+
Review the following lecture script and identify any sections that lack smooth transitions.
|
| 90 |
+
Point out where transitions are missing and suggest improvements.
|
| 91 |
+
|
| 92 |
+
Lecture Script:
|
| 93 |
+
\"\"\"{lecture_script}\"\"\"
|
| 94 |
+
|
| 95 |
+
Return only your feedback and suggestions.
|
| 96 |
+
"""
|
| 97 |
+
return call_openai_chat(prompt)
|
| 98 |
+
|
| 99 |
+
def check_errors(lecture_script: str) -> str:
|
| 100 |
+
prompt = f"""
|
| 101 |
+
Review the following lecture script for any errors including grammar, mathematical inaccuracies, or formatting issues.
|
| 102 |
+
List the errors and provide suggestions for corrections.
|
| 103 |
+
|
| 104 |
+
Lecture Script:
|
| 105 |
+
\"\"\"{lecture_script}\"\"\"
|
| 106 |
+
|
| 107 |
+
Return only your feedback and suggestions.
|
| 108 |
+
"""
|
| 109 |
+
return call_openai_chat(prompt)
|
| 110 |
+
|
| 111 |
+
def check_step_organization(lecture_script: str) -> str:
|
| 112 |
+
prompt = f"""
|
| 113 |
+
Analyze the following lecture script and check whether all problem solutions have their steps clearly enumerated (e.g., Step1, Step2, Step3, etc.).
|
| 114 |
+
If any solutions are missing this clear organization, list those sections and suggest improvements.
|
| 115 |
+
|
| 116 |
+
Lecture Script:
|
| 117 |
+
\"\"\"{lecture_script}\"\"\"
|
| 118 |
+
|
| 119 |
+
Return only your feedback and suggestions.
|
| 120 |
+
"""
|
| 121 |
+
return call_openai_chat(prompt)
|
| 122 |
+
|
| 123 |
+
def refine_lecture_manual(lecture_script: str, transitions_feedback: str, errors_feedback: str, steps_feedback: str) -> str:
|
| 124 |
"""
|
| 125 |
+
Produces a refined version of the lecture script using agent feedback.
|
| 126 |
+
This version may include a separate **Common Mistakes:** section.
|
| 127 |
"""
|
| 128 |
+
prompt = f"""
|
| 129 |
+
You are an expert in educational content design. Below is the original lecture script along with feedback from three specialized agents:
|
| 130 |
+
|
| 131 |
+
--- Original Lecture Script ---
|
| 132 |
+
\"\"\"{lecture_script}\"\"\"
|
| 133 |
+
|
| 134 |
+
--- Feedback ---
|
| 135 |
+
Transitions Feedback:
|
| 136 |
+
{transitions_feedback}
|
| 137 |
+
|
| 138 |
+
Errors Feedback:
|
| 139 |
+
{errors_feedback}
|
| 140 |
+
|
| 141 |
+
Step Organization Feedback:
|
| 142 |
+
{steps_feedback}
|
| 143 |
+
|
| 144 |
+
Using this feedback, provide a refined version of the lecture script that addresses all the issues mentioned.
|
| 145 |
+
Ensure that transitions between sections are smooth, all errors are corrected, and all problem solutions have clearly enumerated steps.
|
| 146 |
+
Include a section labeled **Common Mistakes:** if necessary.
|
| 147 |
+
Return only the refined lecture script.
|
| 148 |
+
"""
|
| 149 |
+
return call_openai_chat(prompt)
|
| 150 |
+
|
| 151 |
+
def add_extra_transitions(lecture_script: str) -> str:
|
| 152 |
"""
|
| 153 |
+
Enhances the refined lecture script by integrating transitions, summaries, and commentary on common mistakes seamlessly into the narrative.
|
| 154 |
+
This function checks if the refined script contains integrated commentary on common mistakes (e.g., "Common Mistake:" or "**Pitfall:**")
|
| 155 |
+
and summary insights (e.g., "Summary:"). If such elements are missing or not well integrated, it weaves them naturally into the text.
|
| 156 |
"""
|
| 157 |
prompt = f"""
|
| 158 |
+
You are provided with a refined lecture script. Your task is to enhance it by:
|
| 159 |
+
1. Adding explicit transitions between steps and sections.
|
| 160 |
+
2. Seamlessly integrating key conclusions, summary insights, and commentary on common mistakes directly into the narrative.
|
| 161 |
+
3. Checking whether the lecture script includes integrated remarks on common mistakes (e.g., "Common Mistake:" or "**Pitfall:**") and summaries (e.g., "Summary:"); if not, embed these elements naturally into the content.
|
| 162 |
+
4. Avoid appending these as separate sections—instead, ensure the final output reads as a unified, continuous lecture script.
|
| 163 |
|
| 164 |
+
Return only the enhanced lecture script.
|
| 165 |
|
| 166 |
+
Lecture Script:
|
| 167 |
+
\"\"\"{lecture_script}\"\"\"
|
|
|
|
|
|
|
| 168 |
"""
|
| 169 |
+
return call_openai_chat(prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
|
| 171 |
+
# --------------------------------------------
|
| 172 |
+
# Sidebar: Input Parameters
|
| 173 |
+
# --------------------------------------------
|
| 174 |
|
| 175 |
+
st.sidebar.header("Input Parameters")
|
| 176 |
|
| 177 |
+
math_subject = st.sidebar.text_input("Math Subject", value="Algebra")
|
| 178 |
+
topic = st.sidebar.text_input("Topic", value="Quadratic Equations")
|
| 179 |
+
difficulty = st.sidebar.selectbox("Difficulty", options=["beginner", "intermediate", "advanced"], index=1)
|
| 180 |
+
speaking_style = st.sidebar.text_input("Speaking Style", value="engaging and clear")
|
| 181 |
+
example1 = st.sidebar.text_area("Example Problem", value="Solve x^2 - 5x + 6 = 0")
|
| 182 |
+
problem1 = st.sidebar.text_area("Problem 1", value="Find the roots of 2x^2 - 4x - 6 = 0")
|
| 183 |
+
wordproblem = st.sidebar.text_area("Word Problem", value="A projectile is launched with a height given by h(t) = -4.9t^2 + 20t + 5. Determine when it reaches the ground.")
|
| 184 |
+
prev_lecture = st.sidebar.text_area("Previous Lecture Reference", value="We covered linear equations and basic factoring techniques.")
|
| 185 |
|
| 186 |
+
# --------------------------------------------
|
| 187 |
+
# Main Area: Editable Lecture Prompt Template
|
| 188 |
+
# --------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
|
| 190 |
+
st.title("Lecture Script Generator and Refinement Interface")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
|
| 192 |
+
# Build the default prompt based on current sidebar inputs.
|
| 193 |
+
default_prompt = default_prompt_builder(
|
| 194 |
+
math_subject, topic, difficulty, speaking_style, example1, problem1, wordproblem, prev_lecture
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
)
|
| 196 |
|
| 197 |
+
# Initialize session state for custom prompt if not set.
|
| 198 |
+
if "custom_prompt" not in st.session_state:
|
| 199 |
+
st.session_state.custom_prompt = default_prompt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
|
| 201 |
+
st.subheader("Lecture Prompt Template (Editable)")
|
| 202 |
+
st.write("This is the template with numbered sections and placeholders. Use the button below to update it based on the current input parameters, or edit it directly.")
|
| 203 |
+
if st.button("Update Prompt Template with current placeholder values"):
|
| 204 |
+
st.session_state.custom_prompt = default_prompt
|
| 205 |
|
| 206 |
+
# Editable text area for the prompt template.
|
| 207 |
+
custom_prompt = st.text_area("Edit Lecture Prompt Template", value=st.session_state.custom_prompt, height=400, key="custom_prompt_main")
|
| 208 |
+
st.session_state.custom_prompt = custom_prompt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
|
| 210 |
+
# --------------------------------------------
|
| 211 |
+
# Lecture Script Generation, Agent Feedback, and Refinement
|
| 212 |
+
# --------------------------------------------
|
| 213 |
+
|
| 214 |
+
if st.button("Generate Lecture Script"):
|
| 215 |
+
with st.spinner("Generating lecture script and running agent checks..."):
|
| 216 |
+
# 1. Generate the initial lecture script.
|
| 217 |
+
lecture_script = call_openai_chat(custom_prompt)
|
| 218 |
+
# 2. Run all agent feedback functions.
|
| 219 |
+
transitions_feedback = check_transitions(lecture_script)
|
| 220 |
+
errors_feedback = check_errors(lecture_script)
|
| 221 |
+
steps_feedback = check_step_organization(lecture_script)
|
| 222 |
+
# 3. Produce the refined lecture script using the agent feedback.
|
| 223 |
+
refined_script = refine_lecture_manual(lecture_script, transitions_feedback, errors_feedback, steps_feedback)
|
| 224 |
+
# 4. Produce the scriptified lecture script by further processing the refined script with extra transitions.
|
| 225 |
+
scriptified_script = add_extra_transitions(refined_script)
|
| 226 |
+
# Save all results in session state.
|
| 227 |
+
st.session_state['lecture_script'] = lecture_script
|
| 228 |
+
st.session_state['transitions_feedback'] = transitions_feedback
|
| 229 |
+
st.session_state['errors_feedback'] = errors_feedback
|
| 230 |
+
st.session_state['steps_feedback'] = steps_feedback
|
| 231 |
+
st.session_state['refined_script'] = refined_script
|
| 232 |
+
st.session_state['scriptified_script'] = scriptified_script
|
| 233 |
+
st.success("Lecture script, agent feedback, and both refined scripts generated!")
|
| 234 |
+
|
| 235 |
+
# --------------------------------------------
|
| 236 |
+
# Editable Lecture Script and Update Agent Feedback
|
| 237 |
+
# --------------------------------------------
|
| 238 |
+
|
| 239 |
+
if "lecture_script" in st.session_state:
|
| 240 |
+
st.subheader("Generated Lecture Script (Editable)")
|
| 241 |
+
lecture_script = st.text_area("Lecture Script", value=st.session_state['lecture_script'], height=300)
|
| 242 |
+
st.session_state['lecture_script'] = lecture_script
|
| 243 |
+
|
| 244 |
+
if st.button("Update Agent Feedback"):
|
| 245 |
+
with st.spinner("Fetching updated agent feedback..."):
|
| 246 |
+
transitions_feedback = check_transitions(lecture_script)
|
| 247 |
+
errors_feedback = check_errors(lecture_script)
|
| 248 |
+
steps_feedback = check_step_organization(lecture_script)
|
| 249 |
+
st.session_state['transitions_feedback'] = transitions_feedback
|
| 250 |
+
st.session_state['errors_feedback'] = errors_feedback
|
| 251 |
+
st.session_state['steps_feedback'] = steps_feedback
|
| 252 |
+
st.success("Agent feedback updated!")
|
| 253 |
+
|
| 254 |
+
# --------------------------------------------
|
| 255 |
+
# Regenerate Refined Scripts if Needed
|
| 256 |
+
# --------------------------------------------
|
| 257 |
+
if "lecture_script" in st.session_state and st.session_state.get("transitions_feedback") is not None:
|
| 258 |
+
if st.button("Regenerate Refined Lecture Scripts"):
|
| 259 |
+
with st.spinner("Regenerating refined lecture scripts..."):
|
| 260 |
+
refined_script = refine_lecture_manual(
|
| 261 |
+
st.session_state["lecture_script"],
|
| 262 |
+
st.session_state.get('transitions_feedback', ''),
|
| 263 |
+
st.session_state.get('errors_feedback', ''),
|
| 264 |
+
st.session_state.get('steps_feedback', '')
|
| 265 |
+
)
|
| 266 |
+
scriptified_script = add_extra_transitions(refined_script)
|
| 267 |
+
st.session_state['refined_script'] = refined_script
|
| 268 |
+
st.session_state['scriptified_script'] = scriptified_script
|
| 269 |
+
st.success("Both refined lecture scripts regenerated!")
|
| 270 |
+
|
| 271 |
+
# --------------------------------------------
|
| 272 |
+
# Display Refined and Scriptified Lectures Side by Side
|
| 273 |
+
# --------------------------------------------
|
| 274 |
+
if "refined_script" in st.session_state and "scriptified_script" in st.session_state:
|
| 275 |
+
st.subheader("Refined and Scriptified Lectures")
|
| 276 |
+
col1, col2 = st.columns(2)
|
| 277 |
+
with col1:
|
| 278 |
+
st.text_area("Refined Lecture Script", value=st.session_state['refined_script'], height=300)
|
| 279 |
+
with col2:
|
| 280 |
+
st.text_area("Scriptified Lecture Script", value=st.session_state['scriptified_script'], height=300)
|
| 281 |
+
|
| 282 |
+
# --------------------------------------------
|
| 283 |
+
# Display Agent Feedback Below
|
| 284 |
+
# --------------------------------------------
|
| 285 |
+
if "transitions_feedback" in st.session_state:
|
| 286 |
+
st.subheader("Agent Feedback")
|
| 287 |
+
st.markdown("**Transitions Feedback:**")
|
| 288 |
+
st.text_area("", value=st.session_state.get('transitions_feedback', ''), height=150)
|
| 289 |
+
st.markdown("**Errors Feedback:**")
|
| 290 |
+
st.text_area("", value=st.session_state.get('errors_feedback', ''), height=150)
|
| 291 |
+
st.markdown("**Step Organization Feedback:**")
|
| 292 |
+
st.text_area("", value=st.session_state.get('steps_feedback', ''), height=150)
|