maazhameed181/NextStepEduBot
0
1import gradio as gr2import os3import requests4 5# Load GROQ API key from environment6GROQ_API_KEY = os.environ.get("GROQ_API_KEY")7GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"8MODEL_NAME = "llama3-8b-8192" # Balanced and fast for Q&A bots9 10# Customize this system prompt based on your bot's role11SYSTEM_PROMPT = (12 "You are a friendly and helpful travel advisor. "13 "You answer user questions about travel destinations, planning, and tips in a clear and engaging way."14)15 16def query_groq(message, chat_history):17 headers = {18 "Authorization": f"Bearer {GROQ_API_KEY}",19 "Content-Type": "application/json"20 }21 22 messages = [{"role": "system", "content": SYSTEM_PROMPT}]23 for user, bot in chat_history:24 messages.append({"role": "user", "content": user})25 messages.append({"role": "assistant", "content": bot})26 messages.append({"role": "user", "content": message})27 28 response = requests.post(GROQ_API_URL, headers=headers, json={29 "model": MODEL_NAME,30 "messages": messages,31 "temperature": 0.732 })33 34 if response.status_code == 200:35 reply = response.json()["choices"][0]["message"]["content"]36 return reply37 else:38 return f"Error {response.status_code}: {response.text}"39 40def respond(message, chat_history):41 bot_reply = query_groq(message, chat_history)42 chat_history.append((message, bot_reply))43 return "", chat_history44 45with gr.Blocks() as demo:46 gr.Markdown("## Education Advisor Chatbot")47 chatbot = gr.Chatbot()48 msg = gr.Textbox(label="Ask a question")49 clear = gr.Button("Clear Chat")50 state = gr.State([])51 52 msg.submit(respond, [msg, state], [msg, chatbot])53 clear.click(lambda: ([], []), None, [chatbot, state])54 55demo.launch()56 