Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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. 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 |
+
FEW_SHOT_EXAMPLES = """
|
| 30 |
+
<example_1>
|
| 31 |
+
USER_INPUT: "The Great Gatsby shows that money is bad. Gatsby has a lot of money but he is sad."
|
| 32 |
+
USER_GOAL: "Argument Analysis"
|
| 33 |
+
AI_RESPONSE:
|
| 34 |
+
**Critique:** Your point is valid, but the sentence structure
|
| 35 |
+
is a bit simple and relies on "telling" rather than "showing."
|
| 36 |
+
You are using two independent clauses joined loosely.
|
| 37 |
+
|
| 38 |
+
**Lesson - Complex Sentences:** Try using a subordinating
|
| 39 |
+
conjunction (like "although," "while," or "despite") to show
|
| 40 |
+
the relationship between wealth and happiness.
|
| 41 |
+
|
| 42 |
+
**Structural Example (Topic: Sports):** Instead of saying "The
|
| 43 |
+
team won the game but they played bad," a stronger writer might
|
| 44 |
+
say: *"Despite securing a victory on the scoreboard, the team's
|
| 45 |
+
defensive breakdown revealed fundamental weaknesses in their strategy."*
|
| 46 |
+
|
| 47 |
+
**Try it:** Can you rewrite your sentence about Gatsby using the word "Although" or "Despite"?
|
| 48 |
+
</example_1>
|
| 49 |
+
|
| 50 |
+
<example_2>
|
| 51 |
+
USER_INPUT: "I think that climate change is a big problem because it makes the weather hot."
|
| 52 |
+
USER_GOAL: "Vocabulary & Tone"
|
| 53 |
+
AI_RESPONSE:
|
| 54 |
+
**Critique:** The phrase "big problem" is vague, and "I think"
|
| 55 |
+
is unnecessary (the reader knows it's your essay). Academic writing requires precision.
|
| 56 |
+
|
| 57 |
+
**Lesson - Precise Vocabulary:** Replace general words with
|
| 58 |
+
specific terms that describe the *scale* or *nature* of the problem.
|
| 59 |
+
|
| 60 |
+
**Structural Example (Topic: Cooking):** Instead of
|
| 61 |
+
saying "I think the soup was bad because it was too salty,"
|
| 62 |
+
a critic would write: *"The broth's overwhelming salinity
|
| 63 |
+
completely masked the delicate flavors of the vegetables."*
|
| 64 |
+
|
| 65 |
+
**Try it:** Look at your sentence. How can you replace "big problem"
|
| 66 |
+
with a word that describes *how* climate change affects the planet
|
| 67 |
+
(e.g., catastrophic, systemic, accelerating)?
|
| 68 |
+
</example_2>
|
| 69 |
+
"""
|
| 70 |
+
|
| 71 |
+
SYSTEM_PROMPT = f"""
|
| 72 |
+
You are an expert Writing Coach for high school students.
|
| 73 |
+
Your goal is to teach writing mechanics, logic, and rhetoric without rewriting the student's essay for them.
|
| 74 |
+
|
| 75 |
+
INSTRUCTIONS:
|
| 76 |
+
1. Analyze the student's text based on their selected Focus Area.
|
| 77 |
+
2. Identify the top 1-2 weaknesses.
|
| 78 |
+
3. For every weakness you identify, you must provide a **"Structural Example"**.
|
| 79 |
+
4. CRITICAL: The "Structural Example" must be about a COMPLETELY DIFFERENT TOPIC than the student's essay. If they write about History, give an example about Cooking. If they write about Literature, give an example about Technology.
|
| 80 |
+
5. Never rewrite their actual sentence. Only show them the *pattern* of a better sentence.
|
| 81 |
+
6. Be encouraging but rigorous. Treat them like smart young adults.
|
| 82 |
+
|
| 83 |
+
Here are examples of how you should respond (Few-Shot Training):
|
| 84 |
+
{FEW_SHOT_EXAMPLES}
|
| 85 |
+
"""
|
| 86 |
+
|
| 87 |
+
# 3. The UI Layout
|
| 88 |
+
st.title("🎓 Digital Writing Coach")
|
| 89 |
+
st.markdown("Paste your draft below. I will critique it and offer advice, but I won't rewrite it for you!")
|
| 90 |
+
|
| 91 |
+
# Two columns: Input and Settings
|
| 92 |
+
col1, col2 = st.columns([2, 1])
|
| 93 |
+
|
| 94 |
+
with col1:
|
| 95 |
+
user_text = st.text_area("Your Essay/Draft", height=400, placeholder="Paste your writing here...")
|
| 96 |
+
|
| 97 |
+
with col2:
|
| 98 |
+
st.header("Feedback Settings")
|
| 99 |
+
focus_area = st.selectbox(
|
| 100 |
+
"What should I focus on?",
|
| 101 |
+
["General Critique", "Grammar & Punctuation", "Argument & Logic", "Clarity & Flow"]
|
| 102 |
+
)
|
| 103 |
+
grade_level = st.select_slider("Grade Level", options=["9th", "10th", "11th", "12th"])
|
| 104 |
+
|
| 105 |
+
analyze_btn = st.button("Analyze My Writing", type="primary", use_container_width=True)
|
| 106 |
+
|
| 107 |
+
# 4. The Logic
|
| 108 |
+
if analyze_btn and user_text:
|
| 109 |
+
with st.spinner("Analyzing your rhetorical strategies..."):
|
| 110 |
+
try:
|
| 111 |
+
# Construct the specific user prompt
|
| 112 |
+
user_prompt = f"Grade Level: {grade_level}\nFocus Area: {focus_area}\n\nStudent Text:\n{user_text}"
|
| 113 |
+
|
| 114 |
+
response = client.chat.completions.create(
|
| 115 |
+
model="gpt-4o", # High-spec model is crucial for nuance
|
| 116 |
+
messages=[
|
| 117 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
| 118 |
+
{"role": "user", "content": user_prompt}
|
| 119 |
+
],
|
| 120 |
+
temperature=0.7 # Slight creativity for "teacher" persona, but grounded
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
feedback = response.choices[0].message.content
|
| 124 |
+
|
| 125 |
+
# 5. Display Feedback
|
| 126 |
+
st.markdown("---")
|
| 127 |
+
st.subheader("📝 Coach's Feedback")
|
| 128 |
+
st.markdown(feedback)
|
| 129 |
+
|
| 130 |
+
except Exception as e:
|
| 131 |
+
st.error(f"An error occurred: {e}")
|