student
Browse files
app.py
ADDED
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import streamlit as st
|
3 |
+
from groq import Groq
|
4 |
+
|
5 |
+
# Set up Streamlit page configuration
|
6 |
+
st.set_page_config(
|
7 |
+
page_title="Writing Assistant",
|
8 |
+
page_icon="✍️",
|
9 |
+
layout="wide",
|
10 |
+
)
|
11 |
+
|
12 |
+
# Streamlit UI
|
13 |
+
st.title("✍️ Writing Assistant for IELTS, DET, and TOEFL")
|
14 |
+
|
15 |
+
# Prompt user to input the API key (Secure way)
|
16 |
+
api_key = st.text_input(
|
17 |
+
"Enter your Groq API Key:",
|
18 |
+
type="password",
|
19 |
+
help="Enter your Groq API key to access the feedback model."
|
20 |
+
)
|
21 |
+
|
22 |
+
# Verify API key
|
23 |
+
if not api_key:
|
24 |
+
st.warning("Please enter your Groq API key to proceed.")
|
25 |
+
st.stop()
|
26 |
+
|
27 |
+
# Initialize Groq client
|
28 |
+
try:
|
29 |
+
client = Groq(api_key=api_key)
|
30 |
+
except Exception as e:
|
31 |
+
st.error(f"Failed to initialize Groq client: {e}")
|
32 |
+
st.stop()
|
33 |
+
|
34 |
+
# Essay plans (30, 45, and 60 days)
|
35 |
+
plans = {
|
36 |
+
"30 Days": ["Essay Topic 1", "Essay Topic 2", "Essay Topic 3"],
|
37 |
+
"45 Days": ["Essay Topic 1", "Essay Topic 2", "Essay Topic 3", "Essay Topic 4"],
|
38 |
+
"60 Days": ["Essay Topic 1", "Essay Topic 2", "Essay Topic 3", "Essay Topic 4", "Essay Topic 5"]
|
39 |
+
}
|
40 |
+
|
41 |
+
# Select plan
|
42 |
+
selected_plan = st.selectbox("Choose your plan:", list(plans.keys()), index=0)
|
43 |
+
topics = plans[selected_plan]
|
44 |
+
|
45 |
+
# Day selector dropdown
|
46 |
+
selected_day = st.slider("Select your current day:", 1, len(topics))
|
47 |
+
current_topic = topics[selected_day - 1]
|
48 |
+
|
49 |
+
# Show the current essay topic
|
50 |
+
st.subheader(f"📝 Today's Essay Topic: {current_topic}")
|
51 |
+
|
52 |
+
# Show upcoming topics
|
53 |
+
st.markdown("#### Upcoming Topics:")
|
54 |
+
for i, topic in enumerate(topics[selected_day:], start=selected_day + 1):
|
55 |
+
st.write(f"Day {i}: {topic}")
|
56 |
+
|
57 |
+
# Essay submission
|
58 |
+
st.markdown("### Write Your Essay Below:")
|
59 |
+
essay_input = st.text_area("Your essay:", placeholder="Write your essay here...", height=300)
|
60 |
+
|
61 |
+
# Submit button
|
62 |
+
if st.button("Submit for Feedback"):
|
63 |
+
if not essay_input.strip():
|
64 |
+
st.error("Please write your essay before submitting.")
|
65 |
+
else:
|
66 |
+
# Define the system prompt for the Groq model
|
67 |
+
system_prompt = """
|
68 |
+
You are an experienced academic English instructor. You must provide feedback as an English teacher would, giving concise but effective advice to improve the student's writing.
|
69 |
+
Focus on specific, actionable feedback without lengthy explanations.
|
70 |
+
|
71 |
+
For each error or area of improvement:
|
72 |
+
1. Identify grammar or vocabulary mistakes by saying: "Replace 'X' with 'Y' (Type: Grammar/Vocabulary)".
|
73 |
+
2. For cohesion, sentence structure, or other improvements, suggest: "Rephrase 'X' as 'Y' for better flow".
|
74 |
+
3. Provide concise suggestions based on English proficiency levels (A1 to C1).
|
75 |
+
4. Do not generate vague or lengthy answers; avoid unnecessary technical jargon.
|
76 |
+
5. Limit feedback to 4-5 actionable points, focusing on the most important improvements.
|
77 |
+
|
78 |
+
Essay Analysis Areas: Grammar, cohesion, sentence structure, vocabulary, and effective use of simple, compound, and complex sentences.
|
79 |
+
"""
|
80 |
+
|
81 |
+
# Send the essay to the Groq model
|
82 |
+
try:
|
83 |
+
response = client.chat.completions.create(
|
84 |
+
messages=[
|
85 |
+
{"role": "system", "content": system_prompt},
|
86 |
+
{"role": "user", "content": essay_input}
|
87 |
+
],
|
88 |
+
model="llama3-8b-8192",
|
89 |
+
)
|
90 |
+
|
91 |
+
# Display feedback
|
92 |
+
feedback = response.choices[0].message.content
|
93 |
+
st.success("✅ Feedback Received!")
|
94 |
+
st.markdown("### Model Feedback:")
|
95 |
+
st.write(feedback)
|
96 |
+
|
97 |
+
except Exception as e:
|
98 |
+
st.error(f"Failed to get feedback from the model: {e}")
|