Talha812 commited on
Commit
6bb0fd3
·
verified ·
1 Parent(s): d814586

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +117 -0
app.py ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from groq import Groq
4
+
5
+ # Load Groq API Key (HF Spaces uses environment variables)
6
+ client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
7
+
8
+ def chat_with_groq(user_input, chat_history):
9
+ if not user_input.strip():
10
+ return chat_history, ""
11
+
12
+ messages = []
13
+ for u, a in chat_history:
14
+ messages += [
15
+ {"role": "user", "content": u},
16
+ {"role": "assistant", "content": a},
17
+ ]
18
+
19
+ messages.append({"role": "user", "content": user_input})
20
+
21
+ try:
22
+ chat_completion = client.chat.completions.create(
23
+ model="llama-3.3-70b-versatile",
24
+ messages=messages,
25
+ )
26
+ assistant_response = chat_completion.choices[0].message.content
27
+ chat_history.append((user_input, assistant_response))
28
+
29
+ except Exception as e:
30
+ chat_history.append((user_input, f"❌ Error: {e}"))
31
+
32
+ return chat_history, ""
33
+
34
+ # UI Styling (Accessible + Responsive)
35
+ custom_css = """
36
+ .gradio-container {
37
+ max-width: 1000px;
38
+ margin: auto;
39
+ padding: 16px;
40
+ }
41
+
42
+ body {
43
+ background: #f8f9fa;
44
+ color: #1f2937;
45
+ font-family: 'Inter', sans-serif;
46
+ }
47
+
48
+ h1 {
49
+ color: #0b4f99;
50
+ font-weight: bold;
51
+ font-size: 2rem;
52
+ text-align: center;
53
+ margin-bottom: 12px;
54
+ }
55
+
56
+ #chatbot {
57
+ background: #ffffff;
58
+ border: 1px solid #d1d5db;
59
+ border-radius: 12px;
60
+ padding: 12px;
61
+ box-shadow: 0 2px 8px rgba(0,0,0,0.1);
62
+ }
63
+
64
+ .message.user {
65
+ background: #0b4f99;
66
+ color: #ffffff;
67
+ border-radius: 16px;
68
+ padding: 8px;
69
+ }
70
+
71
+ .message.bot {
72
+ background: #e5e7eb;
73
+ color: #1f2937;
74
+ border-radius: 16px;
75
+ padding: 8px;
76
+ }
77
+
78
+ textarea {
79
+ width: 100%;
80
+ border: 2px solid #6b7280;
81
+ border-radius: 8px;
82
+ padding: 8px;
83
+ font-size: 1rem;
84
+ color: #1f2937;
85
+ }
86
+
87
+ button {
88
+ background: #0b4f99;
89
+ color: #ffffff;
90
+ font-weight: bold;
91
+ border-radius: 8px;
92
+ padding: 12px 20px;
93
+ }
94
+ button:hover, button:focus {
95
+ background: #093c77;
96
+ }
97
+ """
98
+
99
+ with gr.Blocks(css=custom_css) as demo:
100
+ gr.Markdown("<h1>🌐 Groq ChatBot</h1>")
101
+ chatbot = gr.Chatbot(elem_id="chatbot", height=450)
102
+
103
+ with gr.Row():
104
+ msg = gr.Textbox(
105
+ placeholder="💬 Type your message here...",
106
+ show_label=False,
107
+ scale=4,
108
+ )
109
+ send = gr.Button("Send", scale=1)
110
+
111
+ clear = gr.Button("Clear Chat")
112
+
113
+ send.click(chat_with_groq, [msg, chatbot], [chatbot, msg])
114
+ msg.submit(chat_with_groq, [msg, chatbot], [chatbot, msg])
115
+ clear.click(lambda: [], None, chatbot)
116
+
117
+ demo.launch()