Talha812 commited on
Commit
cfdc4be
Β·
verified Β·
1 Parent(s): e805a14

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -0
app.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from groq import Groq
4
+ from datetime import datetime
5
+
6
+ # ============ Load API Key ============
7
+ # In Hugging Face β†’ Settings β†’ Repository secrets β†’ Add "GROQ_API_KEY"
8
+ api_key = os.getenv("GROQ_API_KEY")
9
+ if not api_key:
10
+ raise ValueError("❌ GROQ_API_KEY not found. Please add it in Hugging Face Space β†’ Settings β†’ Repository secrets.")
11
+
12
+ # Initialize Groq client
13
+ client = Groq(api_key=api_key)
14
+
15
+ # Function to send prompt to Groq API
16
+ def ask_groq(user_message, model="openai/gpt-oss-120b"):
17
+ try:
18
+ response = client.chat.completions.create(
19
+ messages=[{"role": "user", "content": user_message}],
20
+ model=model,
21
+ )
22
+ return response.choices[0].message.content
23
+ except Exception as e:
24
+ return f"⚠️ Error: {str(e)}"
25
+
26
+ # Function to handle chat responses
27
+ def respond(message, chat_history):
28
+ bot_reply = ask_groq(message)
29
+ chat_history.append((message, bot_reply))
30
+ return chat_history, ""
31
+
32
+ # ============ Build Gradio UI ============
33
+ with gr.Blocks(theme=gr.themes.Soft(primary_hue="purple", secondary_hue="cyan")) as demo:
34
+ gr.Markdown(
35
+ """
36
+ <div style="text-align:center; font-size:2em; font-weight:700; color:#9333ea;">
37
+ ⚑ Groq ChatBot ⚑
38
+ </div>
39
+ <p style="text-align:center; color:#64748b;">
40
+ A Gen-Z styled AI chat app powered by Groq LLMs.
41
+ </p>
42
+ """
43
+ )
44
+
45
+ chatbot = gr.Chatbot(
46
+ height=500,
47
+ bubble_full_width=False,
48
+ avatar_images=(
49
+ "https://cdn-icons-png.flaticon.com/512/1077/1077012.png", # user avatar
50
+ "https://cdn-icons-png.flaticon.com/512/4712/4712109.png", # bot avatar
51
+ ),
52
+ )
53
+
54
+ with gr.Row():
55
+ msg = gr.Textbox(
56
+ placeholder="Type your message...",
57
+ show_label=False,
58
+ scale=8,
59
+ container=False,
60
+ )
61
+ send = gr.Button("Send πŸš€", variant="primary", scale=1)
62
+
63
+ send.click(respond, inputs=[msg, chatbot], outputs=[chatbot, msg])
64
+ msg.submit(respond, inputs=[msg, chatbot], outputs=[chatbot, msg])
65
+
66
+ gr.Markdown(
67
+ f"""
68
+ <div style="text-align:center; color:#a3a3a3; margin-top:10px;">
69
+ <small>Powered by Groq β€’ {datetime.now().year}</small>
70
+ </div>
71
+ """
72
+ )
73
+
74
+ # Run app
75
+ if __name__ == "__main__":
76
+ demo.launch()