CoolFace
Apppublic

Ahmad-Muavia/Programming-Tutor

sourceHugging Facemitupdated 9mo agoView on Hugging Face
0likes
app.py110 linesDownload Raw Back to root
1import gradio as gr2import os3import requests4 5# ===============================6# CONFIG7# ===============================8 9GROQ_API_KEY = os.environ.get("GROQ_API_KEY")10 11if not GROQ_API_KEY:12    raise ValueError("GROQ_API_KEY not found in environment variables")13 14GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"15MODEL_NAME = "llama3-8b-8192"16 17# ===============================18# SYSTEM PROMPT (CodeMentor AI)19# ===============================20 21BASE_SYSTEM_PROMPT = """22You are CodeMentor AI, a strict but supportive programming mentor.23 24Your goal is to help users truly understand programming concepts instead of blindly copying solutions.25 26Behavior rules:27- Explain concepts step by step using simple language first, then technical terms.28- Point out logical mistakes, misconceptions, and bad coding practices directly.29- Encourage reasoning, debugging, and problem-solving.30- Avoid unnecessary verbosity and fluff.31- When code is needed, keep it minimal and focused on the concept.32- Maintain a professional, calm, no-nonsense tone.33 34You specialize in programming fundamentals, debugging, problem solving, and clean coding practices.35"""36 37# ===============================38# GROQ QUERY FUNCTION39# ===============================40 41def query_groq(user_message, chat_history, level):42    headers = {43        "Authorization": f"Bearer {GROQ_API_KEY}",44        "Content-Type": "application/json"45    }46 47    # Adjust explanation depth48    level_instruction = f"Explanation level: {level}. Adjust depth accordingly."49 50    messages = [51        {"role": "system", "content": BASE_SYSTEM_PROMPT + "\n" + level_instruction}52    ]53 54    for user, bot in chat_history:55        messages.append({"role": "user", "content": user})56        messages.append({"role": "assistant", "content": bot})57 58    messages.append({"role": "user", "content": user_message})59 60    payload = {61        "model": MODEL_NAME,62        "messages": messages,63        "temperature": 0.764    }65 66    response = requests.post(GROQ_API_URL, headers=headers, json=payload)67 68    if response.status_code == 200:69        return response.json()["choices"][0]["message"]["content"]70    else:71        return f"Error {response.status_code}: {response.text}"72 73# ===============================74# RESPONSE HANDLER75# ===============================76 77def respond(message, chat_history, level):78    reply = query_groq(message, chat_history, level)79    chat_history.append((message, reply))80    return "", chat_history81 82# ===============================83# GRADIO UI84# ===============================85 86with gr.Blocks() as demo:87    gr.Markdown("## ๐Ÿ‘จโ€๐Ÿ’ป CodeMentor AI\n*A strict programming mentor that teaches logic, not shortcuts.*")88 89    chatbot = gr.Chatbot(height=400)90 91    with gr.Row():92        msg = gr.Textbox(93            label="Ask a programming question",94            placeholder="e.g., Why is my loop running infinitely?"95        )96 97    with gr.Row():98        level = gr.Dropdown(99            choices=["Beginner", "Intermediate", "Advanced"],100            value="Beginner",101            label="Explanation Level"102        )103 104    clear = gr.Button("Clear Chat")105    state = gr.State([])106 107    msg.submit(respond, [msg, state, level], [msg, chatbot])108    clear.click(lambda: ([], []), None, [chatbot, state])109 110demo.launch()