File size: 2,772 Bytes
4d342a7
 
 
 
 
 
 
 
 
e386127
4d342a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# -*- coding: utf-8 -*-
"""app.ipynb

Automatically generated by Colab.

Original file is located at
    https://colab.research.google.com/drive/1g3WXzYMdAfSHH2i3i1Ezy2DQUYYifFJI
"""

#!pip install groq gradio

import os
from groq import Groq
import gradio as gr

# ---------- SET YOUR API KEY ----------
# Option 1: Set as environment variable before running:
#   export GROQ_API_KEY="your_api_key_here"
# Option 2: Put it directly here (less safe):
# os.environ["GROQ_API_KEY"] = "your_api_key_here"

#the below 3 lines is for colab, comment if you are using HF
#from google.colab import userdata
#GROQ_API_KEY = userdata.get('GROQ_API_KEY')
#client = Groq(api_key=GROQ_API_KEY)

#the below 1 lines is for HF, comment if you are using colab
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))

# Choose a Llama model available on Groq, e.g.:
MODEL_NAME = "llama-3.3-70b-versatile"   # check console for latest names


def chat_with_groq(message, history):
    """
    message: latest user input (string)
    history: list of [user, assistant] pairs from Gradio
    returns: assistant reply (string)
    """

    # Convert Gradio history into Groq-style messages
    messages = []
    for user_msg, bot_msg in history:
        messages.append({"role": "user", "content": user_msg})
        if bot_msg is not None:
            messages.append({"role": "assistant", "content": bot_msg})

    # Add latest user message
    messages.append({"role": "user", "content": message})

    # Call Groq chat completion
    response = client.chat.completions.create(
        model=MODEL_NAME,
        messages=messages,
        temperature=0.7,
        max_tokens=512,
    )

    reply = response.choices[0].message.content
    return reply


# ---------- GRADIO UI ----------
with gr.Blocks() as demo:
    gr.Markdown("# 💬 Groq Llama Chatbot")
    gr.Markdown(
        "Chat with a Llama model served through the Groq API. "
        "Make sure you set your `GROQ_API_KEY` before running."
    )

    chatbot = gr.Chatbot(height=400)
    msg = gr.Textbox(label="Type your message here")
    clear = gr.Button("Clear")

    def user_send(user_message, chat_history):
        # Append user message to history; bot response handled by second fn
        chat_history = chat_history + [[user_message, None]]
        return "", chat_history

    def bot_reply(chat_history):
        user_message = chat_history[-1][0]
        bot_answer = chat_with_groq(user_message, chat_history[:-1])
        chat_history[-1][1] = bot_answer
        return chat_history

    msg.submit(user_send, [msg, chatbot], [msg, chatbot]).then(
        bot_reply, [chatbot], [chatbot]
    )
    clear.click(lambda: None, None, chatbot, queue=False)

# launch app
if __name__ == "__main__":
    demo.launch()