File size: 5,413 Bytes
8bb6d2c |
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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
import gradio as gr
from groq import Groq
# Initialize Groq client with your API key
client = Groq(api_key="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")
def chat_with_groq(message, history, model_choice, temperature, max_tokens):
"""
Function to interact with Groq API
"""
try:
# Prepare conversation history in the format Groq expects
messages = []
# Add conversation history
for human, assistant in history:
messages.append({"role": "user", "content": human})
messages.append({"role": "assistant", "content": assistant})
# Add current message
messages.append({"role": "user", "content": message})
# Create chat completion
chat_completion = client.chat.completions.create(
messages=messages,
model=model_choice,
temperature=temperature,
max_tokens=max_tokens,
top_p=1,
stream=False
)
# Get the response
response = chat_completion.choices[0].message.content
return response
except Exception as e:
return f"Error: {str(e)}"
# Available Groq models
available_models = [
"llama-3.1-8b-instant",
"llama-3.1-70b-versatile",
"mixtral-8x7b-32768",
"gemma2-9b-it"
]
def predict(message, chat_history, model_choice, temperature, max_tokens):
"""
Predict function for Gradio ChatInterface
"""
if not message.strip():
return chat_history
# Get response from Groq
bot_response = chat_with_groq(message, chat_history, model_choice, temperature, max_tokens)
# Add to chat history
chat_history.append((message, bot_response))
return chat_history
# Custom CSS for better styling
custom_css = """
#chatbot {
min-height: 500px;
}
.container {
max-width: 1200px;
margin: auto;
}
"""
with gr.Blocks(
theme=gr.themes.Soft(),
title="Groq AI Chatbot",
css=custom_css
) as demo:
gr.Markdown(
"""
# ๐ค Groq AI Chatbot
Fast AI-powered chatbot powered by Groq API
**Note**: This chatbot uses Groq's inference API for fast responses.
"""
)
# Store chat history
chatbot = gr.Chatbot(
label="Conversation",
height=500,
show_copy_button=True,
elem_id="chatbot"
)
with gr.Row():
msg = gr.Textbox(
label="Your Message",
placeholder="Type your message here and press Enter...",
lines=2,
scale=4,
container=False
)
with gr.Row():
send_btn = gr.Button("Send ๐", variant="primary", size="lg")
clear_btn = gr.Button("Clear Chat ๐๏ธ", variant="secondary")
with gr.Accordion("โ๏ธ Model Settings", open=False):
with gr.Row():
model_choice = gr.Dropdown(
choices=available_models,
value="llama-3.1-8b-instant",
label="Select Model",
info="Choose which AI model to use"
)
with gr.Row():
temperature = gr.Slider(
minimum=0.1,
maximum=1.0,
value=0.7,
step=0.1,
label="Temperature",
info="Controls creativity: Lower = more deterministic, Higher = more creative"
)
max_tokens = gr.Slider(
minimum=100,
maximum=4096,
value=1024,
step=100,
label="Max Tokens",
info="Maximum length of the response"
)
# Function to handle user message
def user_message(user_msg, history):
return "", history + [[user_msg, None]]
# Function to handle bot response
def bot_message(history, model, temp, tokens):
user_msg = history[-1][0]
bot_msg = chat_with_groq(user_msg, history[:-1], model, temp, tokens)
history[-1][1] = bot_msg
return history
# Event handlers
msg.submit(
user_message,
inputs=[msg, chatbot],
outputs=[msg, chatbot]
).then(
bot_message,
inputs=[chatbot, model_choice, temperature, max_tokens],
outputs=[chatbot]
)
send_btn.click(
user_message,
inputs=[msg, chatbot],
outputs=[msg, chatbot]
).then(
bot_message,
inputs=[chatbot, model_choice, temperature, max_tokens],
outputs=[chatbot]
)
clear_btn.click(
lambda: None,
outputs=[chatbot],
queue=False
).then(
lambda: [],
None,
[chatbot]
)
gr.Markdown(
"""
### ๐ก About the Models:
- **Llama 3.1 8B Instant**: Fast and efficient for general conversations
- **Llama 3.1 70B Versatile**: More capable for complex tasks
- **Mixtral 8x7B**: Excellent for coding and reasoning tasks
- **Gemma2 9B**: Balanced performance across various tasks
### โ ๏ธ Important:
- Your conversations are processed through Groq's API
- The API key is embedded in this application
- For personal use only
"""
)
if __name__ == "__main__":
demo.launch(share=True, debug=True) |