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
def chat_fn(user_message, history):
|
| 11 |
+
messages = []
|
| 12 |
+
|
| 13 |
+
for user, bot in history:
|
| 14 |
+
messages.append({"role": "user", "content": user})
|
| 15 |
+
messages.append({"role": "assistant", "content": bot})
|
| 16 |
+
|
| 17 |
+
messages.append({"role": "user", "content": user_message})
|
| 18 |
+
|
| 19 |
+
response = client.chat.completions.create(
|
| 20 |
+
model="llama-3.3-70b-versatile",
|
| 21 |
+
messages=messages
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
bot_reply = response.choices[0].message.content
|
| 25 |
+
history.append((user_message, bot_reply))
|
| 26 |
+
|
| 27 |
+
return history, history
|
| 28 |
+
|
| 29 |
+
custom_css = """
|
| 30 |
+
body {
|
| 31 |
+
background-color: #f8fafc;
|
| 32 |
+
color: #0f172a;
|
| 33 |
+
font-family: system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
.gradio-container {
|
| 37 |
+
width: 100% !important;
|
| 38 |
+
max-width: 1200px !important;
|
| 39 |
+
margin: auto;
|
| 40 |
+
padding: 20px;
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
h3 {
|
| 44 |
+
text-align: center;
|
| 45 |
+
font-weight: 600;
|
| 46 |
+
color: #0f172a;
|
| 47 |
+
margin-bottom: 16px;
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
#chatbot {
|
| 51 |
+
background-color: #ffffff;
|
| 52 |
+
border-radius: 16px;
|
| 53 |
+
padding: 20px;
|
| 54 |
+
min-height: 65vh;
|
| 55 |
+
border: 1px solid #e5e7eb;
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
.message {
|
| 59 |
+
font-size: 16px;
|
| 60 |
+
line-height: 1.6;
|
| 61 |
+
padding: 14px 16px;
|
| 62 |
+
margin-bottom: 12px;
|
| 63 |
+
max-width: 85%;
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
.message.user {
|
| 67 |
+
background-color: #e0e7ff;
|
| 68 |
+
color: #1e3a8a;
|
| 69 |
+
margin-left: auto;
|
| 70 |
+
border-radius: 14px 14px 4px 14px;
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
.message.bot {
|
| 74 |
+
background-color: #f1f5f9;
|
| 75 |
+
color: #0f172a;
|
| 76 |
+
margin-right: auto;
|
| 77 |
+
border-radius: 14px 14px 14px 4px;
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
textarea {
|
| 81 |
+
background-color: #ffffff !important;
|
| 82 |
+
color: #0f172a !important;
|
| 83 |
+
border: 1px solid #d1d5db !important;
|
| 84 |
+
border-radius: 12px !important;
|
| 85 |
+
padding: 14px !important;
|
| 86 |
+
font-size: 16px !important;
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
textarea::placeholder {
|
| 90 |
+
color: #6b7280;
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
button {
|
| 94 |
+
background-color: #2563eb !important;
|
| 95 |
+
color: #ffffff !important;
|
| 96 |
+
border-radius: 12px !important;
|
| 97 |
+
font-size: 15px !important;
|
| 98 |
+
border: none !important;
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
button:hover {
|
| 102 |
+
background-color: #1d4ed8 !important;
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
/* Mobile support */
|
| 106 |
+
@media (max-width: 640px) {
|
| 107 |
+
.message {
|
| 108 |
+
max-width: 100%;
|
| 109 |
+
font-size: 15px;
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
#chatbot {
|
| 113 |
+
min-height: 60vh;
|
| 114 |
+
}
|
| 115 |
+
}
|
| 116 |
+
"""
|
| 117 |
+
|
| 118 |
+
with gr.Blocks(css=custom_css) as demo:
|
| 119 |
+
gr.Markdown("### Groq Chat")
|
| 120 |
+
|
| 121 |
+
chatbot = gr.Chatbot(elem_id="chatbot")
|
| 122 |
+
state = gr.State([])
|
| 123 |
+
|
| 124 |
+
user_input = gr.Textbox(
|
| 125 |
+
placeholder="Type a message"
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
user_input.submit(
|
| 129 |
+
chat_fn,
|
| 130 |
+
inputs=[user_input, state],
|
| 131 |
+
outputs=[chatbot, state]
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
demo.launch()
|