File size: 1,009 Bytes
23ed574
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import gradio as gr
from groq import Groq


def load_api_key():

    api_key = os.getenv("GROQ_API_KEY")

    if api_key:
        return api_key

    try:
        from google.colab import userdata
        api_key = userdata.get("GROQ_API_KEY")
        return api_key
    except:
        pass

    raise ValueError("GROQ_API_KEY not found.")


GROQ_API_KEY = load_api_key()

client = Groq(api_key=GROQ_API_KEY)


def chat_with_ai(message, history):

    chat_completion = client.chat.completions.create(
        messages=[
            {"role": "user", "content": message}
        ],
        model="llama-3.3-70b-versatile",
    )

    return chat_completion.choices[0].message.content


with gr.Blocks() as demo:

    gr.Markdown("# 🤖 Groq AI Chatbot")

    gr.Markdown(
        "AI chatbot powered by **Groq Llama-3.3-70B**."
    )

    chatbot = gr.ChatInterface(
        fn=chat_with_ai,
        textbox=gr.Textbox(
            placeholder="Type your message..."
        )
    )


demo.launch()