sadiaafzaal/programming_chatbot
0
1# app.py2 3import os4import requests5import gradio as gr6 7# Get the Groq API key from secret environment variable8GROQ_API_KEY = os.getenv("GROQ_API_KEY")9 10MODEL_NAME = "llama3-8b-8192"11 12system_prompt = (13 "You are CodeMentor, a friendly tutor who helps beginners learn programming step by step. "14 "Explain clearly with examples, and format important keywords and headings using **bold markdown** to make your response easy to read."15)16 17def chat_with_groq(user_input):18 url = "https://api.groq.com/openai/v1/chat/completions"19 20 headers = {21 "Authorization": f"Bearer {GROQ_API_KEY}",22 "Content-Type": "application/json"23 }24 25 data = {26 "model": MODEL_NAME,27 "messages": [28 {"role": "system", "content": system_prompt},29 {"role": "user", "content": user_input}30 ]31 }32 33 try:34 response = requests.post(url, headers=headers, json=data)35 result = response.json()36 return result['choices'][0]['message']['content']37 except Exception as e:38 return f"โ Error: {str(e)}"39 40# Gradio UI41demo = gr.Interface(42 fn=chat_with_groq,43 inputs=gr.Textbox(lines=2, placeholder="Ask a programming question..."),44 outputs=gr.Markdown(),45 title="๐จโ๐ซ CodeMentor โ Programming Tutor Chatbot (Groq + LLaMA3)",46 description="Learn programming from zero to hero. Powered by Groq API and LLaMA 3."47)48 49# ๐ง Hugging Face-compatible launch50if __name__ == "__main__":51 demo.launch(server_name="0.0.0.0", server_port=7860)52 53 