NavyDevilDoc commited on
Commit
d194098
·
verified ·
1 Parent(s): 74ba5c2

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +73 -0
main.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from openai import OpenAI
3
+
4
+ # 1. Configuration
5
+ st.set_page_config(page_title="High School Writing Coach", layout="wide")
6
+
7
+ # Initialize client (ensure you have your API key in .streamlit/secrets.toml)
8
+ # or set it directly for testing: client = OpenAI(api_key="sk-...")
9
+ if "OPENAI_API_KEY" in st.secrets:
10
+ client = OpenAI(api_key=st.secrets["OPENAI_API_KEY"])
11
+ else:
12
+ st.error("Please set your OpenAI API Key in .streamlit/secrets.toml")
13
+ st.stop()
14
+
15
+ # 2. The "Secret Sauce" - The System Prompt
16
+ # This enforces the "No Rewrite" rule.
17
+ SYSTEM_PROMPT = """
18
+ You are an expert high school English teacher and writing coach.
19
+ Your goal is to help students improve their writing skills, NOT to do the work for them.
20
+
21
+ RULES:
22
+ 1. DO NOT rewrite the student's text.
23
+ 2. If you see a grammatical error, quote the sentence and explain the grammar rule they broke.
24
+ 3. If you see weak argumentation, ask a Socratic question to prompt deeper thinking.
25
+ 4. Structure your feedback in Markdown with clear headings: "General Feedback", "Strengths", and "Areas for Improvement".
26
+ 5. Be encouraging but rigorous. Treat them like smart young adults.
27
+ """
28
+
29
+ # 3. The UI Layout
30
+ st.title("🎓 Digital Writing Coach")
31
+ st.markdown("Paste your draft below. I will critique it and offer advice, but I won't rewrite it for you!")
32
+
33
+ # Two columns: Input and Settings
34
+ col1, col2 = st.columns([2, 1])
35
+
36
+ with col1:
37
+ user_text = st.text_area("Your Essay/Draft", height=400, placeholder="Paste your writing here...")
38
+
39
+ with col2:
40
+ st.header("Feedback Settings")
41
+ focus_area = st.selectbox(
42
+ "What should I focus on?",
43
+ ["General Critique", "Grammar & Punctuation", "Argument & Logic", "Clarity & Flow"]
44
+ )
45
+ grade_level = st.select_slider("Grade Level", options=["9th", "10th", "11th", "12th"])
46
+
47
+ analyze_btn = st.button("Analyze My Writing", type="primary", use_container_width=True)
48
+
49
+ # 4. The Logic
50
+ if analyze_btn and user_text:
51
+ with st.spinner("Analyzing your rhetorical strategies..."):
52
+ try:
53
+ # Construct the specific user prompt
54
+ user_prompt = f"Grade Level: {grade_level}\nFocus Area: {focus_area}\n\nStudent Text:\n{user_text}"
55
+
56
+ response = client.chat.completions.create(
57
+ model="gpt-4o", # High-spec model is crucial for nuance
58
+ messages=[
59
+ {"role": "system", "content": SYSTEM_PROMPT},
60
+ {"role": "user", "content": user_prompt}
61
+ ],
62
+ temperature=0.7 # Slight creativity for "teacher" persona, but grounded
63
+ )
64
+
65
+ feedback = response.choices[0].message.content
66
+
67
+ # 5. Display Feedback
68
+ st.markdown("---")
69
+ st.subheader("📝 Coach's Feedback")
70
+ st.markdown(feedback)
71
+
72
+ except Exception as e:
73
+ st.error(f"An error occurred: {e}")