Affan8 commited on
Commit
d3eebbc
·
verified ·
1 Parent(s): 082cfc1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -62
app.py CHANGED
@@ -2,28 +2,31 @@ import os
2
  import gradio as gr
3
  from groq import Groq
4
 
5
- # -------- API Setup --------
6
  api_key = os.environ.get("GROQ_API")
 
7
  if not api_key:
8
  raise ValueError("No Groq API key found. Please add it as a secret.")
9
 
 
10
  client = Groq(api_key=api_key)
11
- MODEL_NAME = "openai/gpt-oss-safeguard-20b"
12
 
 
13
 
14
- # -------- Chat Function --------
15
  def chat_groq(message, history):
16
  try:
17
  system_prompt = "You are a helpful AI assistant."
18
  messages = [{"role": "system", "content": system_prompt}]
19
 
 
20
  for turn in history:
21
- if isinstance(turn, (list, tuple)) and len(turn) == 2:
22
  user_msg, bot_msg = turn
23
  messages.append({"role": "user", "content": user_msg})
24
  if bot_msg:
25
  messages.append({"role": "assistant", "content": bot_msg})
26
 
 
27
  messages.append({"role": "user", "content": message})
28
 
29
  completion = client.chat.completions.create(
@@ -33,69 +36,33 @@ def chat_groq(message, history):
33
  temperature=0.7
34
  )
35
 
36
- return completion.choices[0].message.content
 
37
 
38
  except Exception as e:
39
  return f"Error: {e}"
40
 
41
 
42
- # -------- CSS Dark Theme --------
43
- custom_css = """
44
- body {
45
- background-color: #0f172a;
46
- color: #e5e7eb;
47
- }
48
- .gradio-container {
49
- background-color: #020617 !important;
50
- }
51
- textarea, input {
52
- background-color: #020617 !important;
53
- color: #e5e7eb !important;
54
- border: 1px solid #1e293b !important;
55
- }
56
- .message.user {
57
- background-color: #1e293b !important;
58
- }
59
- .message.bot {
60
- background-color: #020617 !important;
61
- }
62
- .logo-title {
63
- display: flex;
64
- align-items: center;
65
- }
66
- .logo-title img {
67
- height: 50px;
68
- margin-right: 15px;
69
- }
70
- .logo-title h1 {
71
- font-size: 28px;
72
- color: #e5e7eb;
73
- margin: 0;
74
- }
75
- """
76
-
77
- # -------- UI --------
78
- with gr.Blocks(css=custom_css, title="Affan's GenAI App (Groq)") as demo:
79
-
80
- # Logo + Title
81
- logo_url = "https://upload.wikimedia.org/wikipedia/commons/a/ab/Logo_TV_2015.png" # Replace with your logo
82
- gr.HTML(f"""
83
- <div class="logo-title">
84
- <img src="{logo_url}" alt="Logo">
85
- <h1>Affan's GenAI App</h1>
86
- </div>
87
- """)
88
-
89
- with gr.Blocks(title="Affan's GenAI App (Groq)") as demo:
90
- # Inject CSS manually
91
- gr.HTML(f"<style>{custom_css}</style>")
92
-
93
- gr.ChatInterface(
94
- fn=chat_groq,
95
- title="Chat"
96
  )
97
 
98
- demo.launch()
99
-
 
 
 
 
100
 
101
- demo.launch()
 
2
  import gradio as gr
3
  from groq import Groq
4
 
5
+ # Read API key from environment (Hugging Face Spaces secret)
6
  api_key = os.environ.get("GROQ_API")
7
+
8
  if not api_key:
9
  raise ValueError("No Groq API key found. Please add it as a secret.")
10
 
11
+ # Initialize Groq client
12
  client = Groq(api_key=api_key)
 
13
 
14
+ MODEL_NAME = "openai/gpt-oss-safeguard-20b"
15
 
 
16
  def chat_groq(message, history):
17
  try:
18
  system_prompt = "You are a helpful AI assistant."
19
  messages = [{"role": "system", "content": system_prompt}]
20
 
21
+ # Handle history safely
22
  for turn in history:
23
+ if isinstance(turn, list) and len(turn) == 2:
24
  user_msg, bot_msg = turn
25
  messages.append({"role": "user", "content": user_msg})
26
  if bot_msg:
27
  messages.append({"role": "assistant", "content": bot_msg})
28
 
29
+ # Add latest user message
30
  messages.append({"role": "user", "content": message})
31
 
32
  completion = client.chat.completions.create(
 
36
  temperature=0.7
37
  )
38
 
39
+ reply = completion.choices[0].message.content
40
+ return reply
41
 
42
  except Exception as e:
43
  return f"Error: {e}"
44
 
45
 
46
+ # 🎨 Define a custom theme
47
+ custom_theme = gr.themes.Base(
48
+ primary_hue="blue",
49
+ secondary_hue="violet",
50
+ neutral_hue="slate",
51
+ font=["Source Sans Pro", "Arial", "sans-serif"],
52
+ ).set(
53
+ body_background_fill="#0f172a", # dark navy background
54
+ body_text_color="#f1f5f9", # light text
55
+ button_primary_background_fill="#6366f1", # indigo buttons
56
+ button_primary_text_color="#ffffff",
57
+ input_background_fill="#1e293b", # dark input box
58
+ input_border_color="#6366f1",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  )
60
 
61
+ # Apply theme to ChatInterface
62
+ ui = gr.ChatInterface(
63
+ fn=chat_groq,
64
+ title="Affan ChatBot",
65
+ theme=custom_theme
66
+ )
67
 
68
+ ui.launch()