forked from jswope00/AI-MicroApps
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_mg_script_gen.py
More file actions
203 lines (171 loc) · 7.18 KB
/
app_mg_script_gen.py
File metadata and controls
203 lines (171 loc) · 7.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import streamlit as st
import os
import hashlib
# configuration must be at the top.
st.set_page_config(
page_title="Motion Graphic Script Generator",
page_icon="app_images/construct.webp",
layout="centered",
initial_sidebar_state="expanded"
)
### hash code function for the encryption
def hash_code(input_code):
"""Hashes the access code using SHA-256."""
return hashlib.sha256(input_code.encode()).hexdigest()
### retrieve hash code
ACCESS_CODE_HASH = os.getenv("ACCESS_CODE_HASH")
if not ACCESS_CODE_HASH:
st.error("⚠️ Hashed access code not found. Please set ACCESS_CODE_HASH.")
st.stop()
### Authentication Logic
if "authenticated" not in st.session_state:
st.session_state.authenticated = False
if not st.session_state.authenticated:
st.title("🔒 Access Restricted")
access_code_input = st.text_input("Enter Access Code:", type="password")
if st.button("Submit"):
if hash_code(access_code_input) == ACCESS_CODE_HASH:
st.session_state.authenticated = True
st.rerun()
else:
st.error("Incorrect access code. Please try again.")
st.stop() # Prevent unauthorized access
APP_URL = "https://motion-graphic-script-gen.streamlit.app/"
APP_IMAGE = "construct.webp"
PUBLISHED = True
APP_TITLE = "Motion Graphic Script Generator"
APP_INTRO = """Use this application to generate motion graphic scripts."""
SYSTEM_PROMPT = """Develop and refine academic video scripts (750 - 800 words) for motion graphic videos that blend information and visuals to improve comprehension and retention.
Visuals: Incorporate advanced visual elements like 2.5D animations, high-quality renderings, and immersive simulations to transform static information into dynamic content.
Output format:
Format scripts as a table, with columns for approximate time, text, and visual cues. Because we should have a change in visuals every 15 seconds, I create the script in 15 - 20-second increments, with each increment being a row in the table. You work on the assumption that each minute of video will have 120 words of text.
Scripts will include the following sections:
1) Begin with an engaging hook.
2) Add a paragraph that establishes the relevance of the learning material to real-world practices.
3) Present the key theory or ideas of the content.
4) Provide an explanation of how that links to solving the problems presented at the start.
5) A conclusion that pulls the video together, tying key points explored in the script.
"""
RAG_IMPLEMENTATION = True # Enable RAG integration
SOURCE_DOCUMENT = "rag_docs/ABETSIS_C1_M0_V1.pdf" # Path to your PDF document
# Required Libraries
# Required Libraries
import os
import fitz # PyMuPDF for PDF processing
import openai
# PDF Text Extraction Function
def extract_text_from_pdf(pdf_path):
"""
Extract text from a PDF file using PyMuPDF (fitz).
"""
text = ""
with fitz.open(pdf_path) as pdf:
for page in pdf:
text += page.get_text("text") # Extract plain text from each page
return text
# Prompt Builder Function
def build_user_prompt(user_input):
"""
Dynamically build the user prompt with user-provided inputs and document content.
"""
try:
# Retrieve user inputs
learning_objectives = user_input.get("learning_objectives", "").strip()
learning_content = user_input.get("learning_content", "").strip()
academic_stage = user_input.get("academic_stage_radio", "").strip()
# Debugging: Print retrieved values
print("Learning Objectives:", learning_objectives)
print("Learning Content:", learning_content)
print("Academic Stage:", academic_stage)
# Validate required inputs
if not learning_objectives:
raise ValueError("The 'Learning Objectives' field is required.")
if not learning_content:
raise ValueError("The 'Learning Content' field is required.")
if not academic_stage:
raise ValueError("An 'Academic Stage' must be selected.")
# Load document content for RAG
document_text = ""
if RAG_IMPLEMENTATION and os.path.exists(SOURCE_DOCUMENT):
document_text = extract_text_from_pdf(SOURCE_DOCUMENT)
document_text = document_text[:2000] # Truncate text to fit within token limits
# Construct the user prompt
user_prompt = f"""
{SYSTEM_PROMPT}
Example Template/Training Data:
{document_text}
The motion graphic script should be aligned with the provided objectives: {learning_objectives}.
Base the motion graphic script on the following learning content: {learning_content}.
Please align the motion graphic script to the following academic stage level: {academic_stage}.
"""
return user_prompt
except Exception as e:
raise ValueError(f"Error building prompt: {str(e)}")
# Configuration for the App
PHASES = {
"generate_discussion": {
"name": "Motion Graphic Script Generator",
"fields": {
"learning_objectives": {
"type": "text_area",
"label": "Enter the relevant module-level learning objective(s):",
"height": 500
},
"learning_content": {
"type": "text_area",
"label": "Enter relevant learning content that will serve as the basis for the motion graphic script.",
"height": 500
},
"academic_stage_radio": {
"type": "radio",
"label": "Select the academic stage of the students:",
"options": [
"Lower Primary",
"Middle Primary",
"Upper Primary",
"Lower Secondary",
"Upper Secondary",
"Undergraduate",
"Postgraduate"
]
}
},
"phase_instructions": """
Provide the relevant details (learning objectives, content, and academic stage) to generate a discussion prompt.
""",
"user_prompt": [
{
"condition": {},
"prompt": "The motion graphic should be aligned with the provided objectives: {learning_objectives}."
},
{
"condition": {},
"prompt": "Base the motion graphic script on the following content. {learning_content}"
},
{
"condition": {},
"prompt": "Please align the motion graphic script to the following academic stage level: {academic_stage_radio}."
}
],
"ai_response": True,
"allow_revisions": True,
"show_prompt": True,
"read_only_prompt": False
}
}
PREFERRED_LLM = "gpt-4o"
LLM_CONFIG_OVERRIDE = {"gpt-4o": {
"family": "openai",
"model": "gpt-4o",
"temperature": 0.5,
"top_p": 0.9,
"frequency_penalty": 0.5,
"presence_penalty": 0.3
}}
SIDEBAR_HIDDEN = True
### Logout Button in Sidebar
st.sidebar.button("Logout", on_click=lambda: st.session_state.update({"authenticated": False}))
# Entry Point
from core_logic.main import main
if __name__ == "__main__":
main(config=globals())