File size: 1,981 Bytes
9ee3f6b
 
 
 
26e4452
bc2c65e
26e4452
 
71ce63b
9ee3f6b
26e4452
bc2c65e
9ee3f6b
bc2c65e
 
 
 
 
9ee3f6b
bc2c65e
9ee3f6b
bc2c65e
 
 
9ee3f6b
bc2c65e
 
 
8ea8681
bc2c65e
 
 
8ea8681
e36d0dc
 
 
8565859
 
e36d0dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import gradio as gr
from groq import Groq

# Set up the Groq API client
apikey = os.getenv("apikey")  # Fetch the API key from secrets
if not apikey:
    raise ValueError("API Key is not set. Add it in the Secrets tab.")
client = Groq(api_key=apikey)

# Function to interact with the LLM
def chatbot(user_input, history):
    try:
        # Format conversation history
        messages = [{"role": "user", "content": user} for user, _ in history]
        messages.append({"role": "user", "content": user_input})

        # Send to Groq's API
        chat_completion = client.chat.completions.create(
            messages=messages, model="llama3-8b-8192"
        )
        response = chat_completion.choices[0].message.content
        history.append((user_input, response))
        return history, history
    except Exception as e:
        return history, history + [(user_input, f"An error occurred: {str(e)}")]

# Enhanced Gradio interface
with gr.Blocks(title="The Ultimate Chat Companion") as demo:
    gr.Markdown(
        """
        # 🧠 **Groq LLM Chatbot**
        Meet the Future - Interact with a powerful LLM in real-time.
        """
    )
    with gr.Row():
        # Use a local image file for the logo
        gr.Image(value="chat-companion-high-resolution-logo.png", label="Logo", interactive=False)
        gr.Markdown(
            """
            **Created by ATIF MEHMOOD**  
            _Experience the next generation of AI-powered conversations._  
            """
        )
    chatbot_ui = gr.Chatbot(label="Chat Interface")
    user_input = gr.Textbox(
        placeholder="Type your message here...",
        label="Your Message",
        lines=2,
    )
    submit_btn = gr.Button(value="Send")

    # Bind functions
    history_state = gr.State([])
    submit_btn.click(chatbot, [user_input, history_state], [chatbot_ui, history_state])
    user_input.submit(chatbot, [user_input, history_state], [chatbot_ui, history_state])

demo.launch()