Spaces:
Sleeping
Sleeping
| import os | |
| from groq import Groq | |
| client = Groq(api_key=os.environ.get("GROQ_API_KEY")) | |
| SYSTEM_PROMPTS = { | |
| "π Explain": """ | |
| You are a friendly, patient study tutor. Your job is to explain any topic | |
| clearly to a student who is hearing it for the first time. | |
| Rules: | |
| - Use simple everyday language. Avoid jargon unless you define it immediately. | |
| - Break the explanation into short paragraphs. | |
| - Use a real-world analogy to make the concept concrete. | |
| - End with one "Key takeaway" sentence in bold. | |
| - Keep the total response under 300 words. | |
| """, | |
| "π§ͺ Quiz Me": """ | |
| You are a quiz master helping a student test their understanding. | |
| When given a topic, generate exactly 5 multiple-choice questions. Format: | |
| Q1. [Question] | |
| A) ... B) ... C) ... D) ... | |
| β Answer: [Letter] β [Brief explanation] | |
| Rules: | |
| - Questions should cover different aspects of the topic. | |
| - Only one option should be clearly correct. | |
| - The explanation after the answer must be one sentence. | |
| - Do not repeat options across questions. | |
| """, | |
| "π Summarise": """ | |
| You are an expert note-taker. Summarise the given topic or text for a student | |
| who needs to revise quickly. | |
| Format your summary as: | |
| β’ 3β5 bullet points covering the main ideas | |
| β’ One "Remember this" box at the end (a single memorable fact or formula) | |
| Rules: | |
| - Every bullet must be one concise sentence. | |
| - Use student-friendly language. | |
| - Do not add padding or filler phrases like "In conclusion...". | |
| """, | |
| "ποΈ Flashcards": """ | |
| You are a flashcard generator. Given a topic, produce 6 study flashcards. | |
| Format each card as: | |
| FRONT: [Term or concept] | |
| BACK: [Clear, concise definition or explanation β max 2 sentences] | |
| Rules: | |
| - Cover a variety of sub-topics within the given subject. | |
| - Keep BACK answers short enough to memorise in one reading. | |
| - Number each card (Card 1, Card 2, β¦). | |
| """, | |
| "π Ask a Doubt": """ | |
| You are a helpful teaching assistant. Answer the student's specific question | |
| directly and accurately. | |
| Rules: | |
| - Answer only what is asked. Do not pad with unrelated information. | |
| - If the question has a common misconception, address it briefly. | |
| - Use examples or analogies if they make the answer clearer. | |
| - Keep the response under 200 words. | |
| """, | |
| } | |
| def study_assistant(topic: str, mode: str) -> str: | |
| """ | |
| Sends a prompt to Groq and returns the response text. | |
| Args: | |
| topic: The student's question or topic to study. | |
| mode: One of the keys in SYSTEM_PROMPTS (matches the dropdown). | |
| Returns: | |
| The model's response as a plain string. | |
| """ | |
| if not topic.strip(): | |
| return "β οΈ Please enter a topic or question first." | |
| system_instruction = SYSTEM_PROMPTS.get(mode, SYSTEM_PROMPTS["π Explain"]) | |
| try: | |
| response = client.chat.completions.create( | |
| model="llama-3.1-8b-instant", # Fast, free-tier Groq model | |
| messages=[ | |
| {"role": "system", "content": system_instruction}, | |
| {"role": "user", "content": topic}, | |
| ], | |
| temperature=0.7, # Balanced creativity | |
| max_tokens=1024, # Enough for flashcards / quiz output | |
| ) | |
| return response.choices[0].message.content | |
| except Exception as error: | |
| return f"β API error: {error}\n\nCheck that your GROQ_API_KEY secret is set correctly." | |
| import gradio as gr | |
| with gr.Blocks(title="π Personalized Study Assistant") as demo: | |
| # ββ Header ββββββββββββββββββββββββββββββββββββββββββββββββ | |
| gr.Markdown(""" | |
| # π Personalized Study Assistant | |
| *Powered by Groq (LLaMA 3.1) β’ Built for ADYPU LLM Practicals* | |
| Choose a study mode, enter your topic or question, and hit **Generate**. | |
| """) | |
| # ββ Controls ββββββββββββββββββββββββββββββββββββββββββββββ | |
| with gr.Row(): | |
| mode_selector = gr.Dropdown( | |
| choices=list(SYSTEM_PROMPTS.keys()), | |
| value="π Explain", | |
| label="Study Mode", | |
| info="Pick how you want to interact with the material.", | |
| ) | |
| topic_input = gr.Textbox( | |
| label="Your topic or question", | |
| placeholder="e.g. Newton's laws of motion | What is photosynthesis? | Explain recursion", | |
| lines=2, | |
| ) | |
| generate_btn = gr.Button("β‘ Generate", variant="primary") | |
| # ββ Output ββββββββββββββββββββββββββββββββββββββββββββββββ | |
| output_box = gr.Textbox( | |
| label="Study Assistant Response", | |
| lines=18, | |
| ) | |
| # ββ Mode description cards ββββββββββββββββββββββββββββββββ | |
| with gr.Accordion("βΉοΈ What does each mode do?", open=False): | |
| gr.Markdown(""" | |
| | Mode | What it does | | |
| |------|-------------| | |
| | π Explain | Breaks down any topic using simple language + a real-world analogy | | |
| | π§ͺ Quiz Me | Generates 5 multiple-choice questions with answers & explanations | | |
| | π Summarise | Bullet-point revision notes + a key fact to remember | | |
| | ποΈ Flashcards | 6 FRONT/BACK flashcards ready to memorise | | |
| | π Ask a Doubt | Direct answer to your specific question | | |
| """) | |
| # ββ Example buttons βββββββββββββββββββββββββββββββββββββββ | |
| gr.Examples( | |
| examples=[ | |
| ["Photosynthesis", "π Explain"], | |
| ["Newton's Laws of Motion", "π§ͺ Quiz Me"], | |
| ["The Water Cycle", "π Summarise"], | |
| ["Machine Learning", "ποΈ Flashcards"], | |
| ["What is the difference between RAM and ROM?", "π Ask a Doubt"], | |
| ], | |
| inputs=[topic_input, mode_selector], | |
| label="Try an example", | |
| ) | |
| # ββ Wire up the button ββββββββββββββββββββββββββββββββββββ | |
| generate_btn.click( | |
| fn=study_assistant, | |
| inputs=[topic_input, mode_selector], | |
| outputs=output_box, | |
| ) | |
| # Also trigger on Enter key in the text box | |
| topic_input.submit( | |
| fn=study_assistant, | |
| inputs=[topic_input, mode_selector], | |
| outputs=output_box, | |
| ) | |
| demo.launch(server_name="0.0.0.0", server_port=7860) |