File size: 5,216 Bytes
e415055
 
 
 
 
 
 
 
 
1fcf424
e415055
 
 
 
 
 
 
 
 
 
 
cb26ec7
 
e415055
 
25746ab
 
e415055
 
19e956e
e415055
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# -*- coding: utf-8 -*-
"""app.ipynb

Automatically generated by Colab.

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

#!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"

#from google.colab import userdata
#GROQ_API_KEY = userdata.get('GROQ_API_KEY')

#client = Groq(api_key=GROQ_API_KEY) # this is for colab
#client = Groq(api_key=os.environ.get("GROQ_API_KEY")) #this is for cloud
client = Groq()

# Choose a Llama model available on Groq, e.g.:
MODEL_NAME = "llama-3.1-8b-instant"   # 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()

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"

#from google.colab import userdata #this is for colab
#GROQ_API_KEY = userdata.get('GROQ_API_KEY') #this is for colab

#client = Groq(api_key=GROQ_API_KEY) #this is for colab
client = Groq(api_key=os.environ.get("GROQ_API_KEY")) #this is for cloud

# 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()