| import os |
| import gradio as gr |
| from groq import Groq |
| from dotenv import load_dotenv |
|
|
| load_dotenv() |
|
|
| client = Groq(api_key=os.getenv("GROQ_API_KEY")) |
|
|
| SYSTEM_PROMPT = "You are Nova, a smart and friendly AI assistant. Be concise, helpful, and honest." |
|
|
| def respond(user_message, history): |
| if not user_message.strip(): |
| return "", history |
|
|
| messages = [{"role": "system", "content": SYSTEM_PROMPT}] |
| for msg in history: |
| messages.append({"role": msg["role"], "content": msg["content"]}) |
| messages.append({"role": "user", "content": user_message}) |
|
|
| try: |
| response = client.chat.completions.create( |
| model="llama-3.1-8b-instant", |
| messages=messages, |
| temperature=0.7, |
| max_tokens=1024, |
| ) |
| reply = response.choices[0].message.content |
| except Exception as e: |
| reply = f"Error: {e}" |
|
|
| history.append({"role": "user", "content": user_message}) |
| history.append({"role": "assistant", "content": reply}) |
| return "", history |
|
|
|
|
| def clear(): |
| return [] |
|
|
|
|
| with gr.Blocks(title="Nova AI") as demo: |
| gr.Markdown("# Nova AI\nYour smart, friendly AI assistant.") |
|
|
| chatbot = gr.Chatbot(height=500, type="messages") |
|
|
| msg = gr.Textbox(placeholder="Type your message...", show_label=False) |
|
|
| with gr.Row(): |
| send = gr.Button("Send", variant="primary") |
| clear_btn = gr.Button("Clear") |
|
|
| msg.submit(respond, inputs=[msg, chatbot], outputs=[msg, chatbot]) |
| send.click(respond, inputs=[msg, chatbot], outputs=[msg, chatbot]) |
| clear_btn.click(clear, outputs=[chatbot]) |
|
|
|
|
| if __name__ == "__main__": |
| demo.launch() |