File size: 2,089 Bytes
9ee3f6b
 
 
 
 
0fc1986
9ee3f6b
 
ab34178
9ee3f6b
ab34178
 
 
9ee3f6b
 
ab34178
9ee3f6b
 
 
ab34178
 
9ee3f6b
ab34178
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9ee3f6b
 
 
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
import os
import gradio as gr
from groq import Groq

# Set up Groq API client (ensure GROQ_API_KEY is set in your environment or as a Hugging Face secret for deployment)
client = Groq(api_key=os.getenv("apikey"))

# Function to interact with the LLM using Groq's API
def chatbot(messages):
    try:
        # Extract the latest user message
        user_input = messages[-1][0] if messages else ""
        
        # Send user input to the LLM and get a response
        chat_completion = client.chat.completions.create(
            messages=[{"role": "user", "content": user_input}],
            model="llama3-8b-8192",  # Replace with the desired model
        )
        response = chat_completion.choices[0].message.content
        messages.append((user_input, response))  # Append user input and response to chat
        return messages
    except Exception as e:
        return messages + [(None, f"An error occurred: {str(e)}")]

# Function to reset the chat
def reset_chat():
    return []

# Gradio interface with a chatbot component
with gr.Blocks(theme=gr.themes.Soft()) as iface:
    gr.Markdown(
        """
        # Real-Time Text-to-Text Chatbot
        **by ATIF MEHMOOD**
        
        Chat with an advanced language model using Groq's API. Enjoy real-time interactions!
        """
    )

    with gr.Row():
        chatbot_ui = gr.Chatbot(label="Chat Interface").style(height=400)
    
    with gr.Row():
        user_input = gr.Textbox(
            label="Type Your Message",
            placeholder="Ask me something...",
            lines=1,
            interactive=True,
        )
        send_button = gr.Button("Send", variant="primary")
        clear_button = gr.Button("Clear Chat")

    with gr.Row():
        gr.Examples(
            examples=["Hello!", "Tell me a joke.", "What's the weather like?"],
            inputs=user_input,
        )

    # Link components to functions
    send_button.click(chatbot, inputs=[chatbot_ui], outputs=chatbot_ui)
    clear_button.click(reset_chat, inputs=[], outputs=chatbot_ui)

# Launch the Gradio app
iface.launch()