NavyDevilDoc commited on
Commit
18064f8
ยท
verified ยท
1 Parent(s): cae8a68

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -38
app.py CHANGED
@@ -21,35 +21,26 @@ client = OpenAI(api_key=api_key)
21
 
22
  # 2. System Prompt
23
  # This enforces the "No Rewrite" rule.
24
- SYSTEM_PROMPT = """
25
- You are an expert high school English teacher and writing coach.
26
- Your goal is to help students improve their writing skills, NOT to do the work for them.
27
-
28
- RULES:
29
- 1. DO NOT rewrite the student's text.
30
- 2. If you see a grammatical error, quote the sentence and explain the grammar rule they broke.
31
- 3. If you see weak argumentation, ask a Socratic question to prompt deeper thinking.
32
- 4. Structure your feedback in Markdown with clear headings: "General Feedback", "Strengths", and "Areas for Improvement".
33
- 5. Be encouraging but rigorous. Treat them like smart young adults.
34
- """
35
-
36
  FEW_SHOT_EXAMPLES = """
37
  <example_1>
38
  USER_INPUT: "The Great Gatsby shows that money is bad. Gatsby has a lot of money but he is sad."
39
  USER_GOAL: "Argument Analysis"
40
  AI_RESPONSE:
41
- **Critique:** Your point is valid, but the sentence structure
42
- is a bit simple and relies on "telling" rather than "showing."
43
- You are using two independent clauses joined loosely.
 
44
 
45
  **Lesson - Complex Sentences:** Try using a subordinating
46
- conjunction (like "although," "while," or "despite") to show
47
- the relationship between wealth and happiness.
48
 
49
- **Structural Example (Topic: Sports):** Instead of saying "The
50
- team won the game but they played bad," a stronger writer might
51
- say: *"Despite securing a victory on the scoreboard, the team's
52
- defensive breakdown revealed fundamental weaknesses in their strategy."*
 
53
 
54
  **Try it:** Can you rewrite your sentence about Gatsby using the word "Although" or "Despite"?
55
  </example_1>
@@ -58,34 +49,50 @@ defensive breakdown revealed fundamental weaknesses in their strategy."*
58
  USER_INPUT: "I think that climate change is a big problem because it makes the weather hot."
59
  USER_GOAL: "Vocabulary & Tone"
60
  AI_RESPONSE:
61
- **Critique:** The phrase "big problem" is vague, and "I think"
62
- is unnecessary (the reader knows it's your essay). Academic writing requires precision.
63
-
64
- **Lesson - Precise Vocabulary:** Replace general words with
65
- specific terms that describe the *scale* or *nature* of the problem.
66
-
67
- **Structural Example (Topic: Cooking):** Instead of
68
- saying "I think the soup was bad because it was too salty,"
69
- a critic would write: *"The broth's overwhelming salinity
70
- completely masked the delicate flavors of the vegetables."*
71
-
72
- **Try it:** Look at your sentence. How can you replace "big problem"
73
- with a word that describes *how* climate change affects the planet
74
- (e.g., catastrophic, systemic, accelerating)?
 
 
 
75
  </example_2>
76
  """
77
 
 
 
78
  SYSTEM_PROMPT = f"""
79
  You are an expert Writing Coach for high school students.
80
  Your goal is to teach writing mechanics, logic, and rhetoric without rewriting the student's essay for them.
81
 
82
- INSTRUCTIONS:
 
 
 
 
 
 
 
 
 
 
 
 
83
  1. Analyze the student's text based on their selected Focus Area.
84
  2. Identify the top 1-2 weaknesses.
85
  3. For every weakness you identify, you must provide a **"Structural Example"**.
86
- 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.
87
  5. Never rewrite their actual sentence. Only show them the *pattern* of a better sentence.
88
- 6. Be encouraging but rigorous. Treat them like smart young adults.
89
 
90
  Here are examples of how you should respond (Few-Shot Training):
91
  {FEW_SHOT_EXAMPLES}
@@ -128,11 +135,32 @@ if analyze_btn and user_text:
128
  )
129
 
130
  feedback = response.choices[0].message.content
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131
 
132
  # 5. Display Feedback
133
  st.markdown("---")
134
  st.subheader("๐Ÿ“ Coach's Feedback")
135
  st.markdown(feedback)
 
 
 
 
 
 
 
136
 
137
  except Exception as e:
138
  st.error(f"An error occurred: {e}")
 
21
 
22
  # 2. System Prompt
23
  # This enforces the "No Rewrite" rule.
24
+ # --- 1. DEFINE THE FEW-SHOT EXAMPLES FIRST ---
 
 
 
 
 
 
 
 
 
 
 
25
  FEW_SHOT_EXAMPLES = """
26
  <example_1>
27
  USER_INPUT: "The Great Gatsby shows that money is bad. Gatsby has a lot of money but he is sad."
28
  USER_GOAL: "Argument Analysis"
29
  AI_RESPONSE:
30
+ **Critique:** Your point is valid, but the sentence
31
+ structure is a bit simple and relies on "telling"
32
+ rather than "showing." You are using two independent
33
+ clauses joined loosely.
34
 
35
  **Lesson - Complex Sentences:** Try using a subordinating
36
+ conjunction (like "although," "while," or "despite") to
37
+ show the relationship between wealth and happiness.
38
 
39
+ **Structural Example (Topic: Sports):** Instead of saying
40
+ "The team won the game but they played bad," a stronger writer
41
+ might say: *"Despite securing a victory on the scoreboard, the
42
+ team's defensive breakdown revealed fundamental weaknesses in
43
+ their strategy."*
44
 
45
  **Try it:** Can you rewrite your sentence about Gatsby using the word "Although" or "Despite"?
46
  </example_1>
 
49
  USER_INPUT: "I think that climate change is a big problem because it makes the weather hot."
50
  USER_GOAL: "Vocabulary & Tone"
51
  AI_RESPONSE:
52
+ **Critique:** The phrase "big problem" is vague,
53
+ and "I think" is unnecessary. Academic writing
54
+ requires precision.
55
+
56
+ **Lesson - Precise Vocabulary:** Replace general
57
+ words with specific terms that describe the
58
+ *scale* or *nature* of the problem.
59
+
60
+ **Structural Example (Topic: Cooking):** Instead
61
+ of saying "I think the soup was bad because it was
62
+ too salty," a critic would write: *"The broth's
63
+ overwhelming salinity completely masked the delicate
64
+ flavors of the vegetables."*
65
+
66
+ **Try it:** Look at your sentence. How can you replace
67
+ "big problem" with a word that describes *how* climate
68
+ change affects the planet?
69
  </example_2>
70
  """
71
 
72
+ # --- 2. DEFINE THE MASTER SYSTEM PROMPT ---
73
+ # We inject the few-shot examples at the end.
74
  SYSTEM_PROMPT = f"""
75
  You are an expert Writing Coach for high school students.
76
  Your goal is to teach writing mechanics, logic, and rhetoric without rewriting the student's essay for them.
77
 
78
+ CORE RULES:
79
+ 1. DO NOT rewrite the student's text.
80
+ 2. If you see a grammatical error, quote the sentence and explain the grammar rule they broke.
81
+ 3. Structure your feedback in Markdown with clear headings: "General Feedback", "Strengths", and "Areas for Improvement".
82
+ 4. Be encouraging but rigorous. Treat them like smart young adults.
83
+
84
+ SOCRATIC INSTRUCTIONS (Use when critiquing logic/argument):
85
+ 1. Do not give the answer.
86
+ 2. Ask a question that exposes the gap in the student's reasoning.
87
+ 3. Use the "Counter-Factual" technique: "If X were true, wouldn't Y also happen?"
88
+ 4. Use the "Perspective Shift" technique: "How would a French soldier in 1812 respond to this claim?"
89
+
90
+ INSTRUCTIONS FOR EXAMPLES:
91
  1. Analyze the student's text based on their selected Focus Area.
92
  2. Identify the top 1-2 weaknesses.
93
  3. For every weakness you identify, you must provide a **"Structural Example"**.
94
+ 4. CRITICAL: The "Structural Example" must be about a COMPLETELY DIFFERENT TOPIC than the student's essay.
95
  5. Never rewrite their actual sentence. Only show them the *pattern* of a better sentence.
 
96
 
97
  Here are examples of how you should respond (Few-Shot Training):
98
  {FEW_SHOT_EXAMPLES}
 
135
  )
136
 
137
  feedback = response.choices[0].message.content
138
+
139
+ # --- COST TRACKING LOGIC ---
140
+ # Extract usage data directly from the API response
141
+ usage = response.usage
142
+ input_tokens = usage.prompt_tokens
143
+ output_tokens = usage.completion_tokens
144
+ total_tokens = usage.total_tokens
145
+
146
+ # GPT-4o Pricing (Estimate as of Late 2025)
147
+ # Input: $2.50 / 1M tokens
148
+ # Output: $10.00 / 1M tokens
149
+ input_cost = (input_tokens / 1_000_000) * 2.50
150
+ output_cost = (output_tokens / 1_000_000) * 10.00
151
+ total_cost = input_cost + output_cost
152
 
153
  # 5. Display Feedback
154
  st.markdown("---")
155
  st.subheader("๐Ÿ“ Coach's Feedback")
156
  st.markdown(feedback)
157
+
158
+ # --- DISPLAY METRICS (In a sidebar or expandable box) ---
159
+ with st.expander("๐Ÿ“Š Session Metrics & Cost"):
160
+ col_a, col_b, col_c = st.columns(3)
161
+ col_a.metric("Input Tokens", input_tokens)
162
+ col_b.metric("Output Tokens", output_tokens)
163
+ col_c.metric("Est. Cost", f"${total_cost:.5f}") # 5 decimal places for micro-costs
164
 
165
  except Exception as e:
166
  st.error(f"An error occurred: {e}")