Spaces:
Runtime error
Runtime error
| import os | |
| import random | |
| import streamlit as st | |
| from groq import Groq | |
| # Set up the Groq client with the API key | |
| os.environ["GROQ_API_KEY"] = "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" | |
| client = Groq(api_key=os.environ.get("GROQ_API_KEY")) | |
| # System prompt for generating feedback | |
| system_prompt = """ | |
| You are an expert academic writer with 40 years of experience in providing concise yet effective feedback for essays. Your job is to help students improve their writing for IELTS, DET, and TOEFL exams by analyzing their essays for grammar, cohesion, sentence structure, vocabulary, and sentence complexity. Instead of generic feedback, you provide specific replacements like "replace this with this" for grammar and vocabulary errors, cohesion issues, and sentence structure improvements. | |
| 1. Grammar: Identify and suggest specific corrections concisely. | |
| 2. Cohesion: Suggest rephrasing or replacement to improve flow. | |
| 3. Sentence Structure: Identify if sentences need restructuring or simplification. | |
| 4. Vocabulary: Suggest vocabulary upgrades for conciseness and formality without overused phrases that may appear AI-generated. | |
| 5. Sentence Complexity: Comment on simple, compound, and complex sentence variety. | |
| Use levels to match the student’s proficiency: | |
| - A1 (Beginner): Simplified corrections, avoiding complex structures. | |
| - A2 (Intermediate): Balanced feedback with a mix of simple and compound structures. | |
| - C1 (Advanced): Detailed feedback with nuanced corrections and sophisticated vocabulary suggestions. | |
| Avoid terms such as "dive, discover, uncover, delve, navigate, landscape, comprehensive embrace, unleash, cutting edge, harness." Respond strictly in academic style. Do not add new content; only adjust and improve the student's original writing. | |
| Provide a feedback report with a suggested improved version of the essay. | |
| """ | |
| # Sample essay topics for 30, 45, and 60-day plans | |
| essay_topics_30 = [ | |
| f"Essay Topic {i+1} for 30-day Plan" for i in range(30) | |
| ] | |
| essay_topics_45 = [ | |
| f"Essay Topic {i+1} for 45-day Plan" for i in range(45) | |
| ] | |
| essay_topics_60 = [ | |
| f"Essay Topic {i+1} for 60-day Plan" for i in range(60) | |
| ] | |
| # Function to get feedback from the Groq API | |
| def get_feedback(essay): | |
| chat_completion = client.chat.completions.create( | |
| messages=[ | |
| {"role": "user", "content": essay}, | |
| {"role": "system", "content": system_prompt} | |
| ], | |
| model="llama3-8b-8192", | |
| ) | |
| return chat_completion.choices[0].message.content | |
| # Streamlit UI layout | |
| st.title("Writing Assistant for IELTS, DET, and TOEFL Preparation") | |
| st.write("Get concise and effective feedback on your essays to improve your writing skills.") | |
| # Choose plan for essay topics | |
| st.header("Choose Your Writing Plan") | |
| plan_selection = st.selectbox("Select Your Writing Plan", ["30-Day Plan", "45-Day Plan", "60-Day Plan"]) | |
| # Define the essay topics based on selected plan | |
| if plan_selection == "30-Day Plan": | |
| essay_topics = essay_topics_30 | |
| elif plan_selection == "45-Day Plan": | |
| essay_topics = essay_topics_45 | |
| else: | |
| essay_topics = essay_topics_60 | |
| # Function to suggest a random essay topic | |
| def suggest_random_topic(): | |
| return random.choice(essay_topics) | |
| # Display suggested essay topic | |
| st.subheader("Suggested Essay Topic for Today") | |
| suggested_topic = suggest_random_topic() | |
| st.write(f"**Suggested Topic:** {suggested_topic}") | |
| # Current day selection | |
| st.subheader("Select Your Current Day") | |
| current_day = st.slider("Choose your current day", 1, len(essay_topics)) | |
| # Display current and upcoming essay topics | |
| st.write(f"### Today's Essay Topic: {essay_topics[current_day - 1]}") | |
| st.write("Upcoming Essay Topics:") | |
| for i, topic in enumerate(essay_topics[current_day:current_day + 10], start=1): # Show up to 10 upcoming topics | |
| st.write(f"- **Day {current_day + i}:** {topic}") | |
| # Input Essay Section | |
| st.subheader("Submit Your Essay") | |
| essay_input = st.text_area("Write your essay here:", height=200) | |
| submit_button = st.button("Get Feedback") | |
| # Display feedback on submission | |
| if submit_button and essay_input: | |
| with st.spinner("Analyzing your essay..."): | |
| feedback = get_feedback(essay_input) | |
| st.subheader("Feedback on Your Essay") | |
| st.write(feedback) | |