AbuSaleh28 commited on
Commit
0082045
·
verified ·
1 Parent(s): 9797bc4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -35
app.py CHANGED
@@ -1,43 +1,33 @@
1
- import os
2
-
3
- # Create app.py content (modifying the user's working Colab code slightly for HF Spaces environment variable safety)
4
- app_content = """import gradio as gr
5
  import os
6
  from groq import Groq
 
7
 
8
  # Initialize the Groq client
9
- # On Hugging Face Spaces, set GROQ_API_KEY as a Secret in the Space Settings
10
- api_key = os.environ.get("GROQ_API_KEY")
11
- if not api_key:
12
- raise ValueError("GROQ_API_KEY environment variable not found. Please add it as a Secret in your Space Settings.")
13
-
14
- client = Groq(api_key=api_key)
15
 
16
  # Custom CSS to give it a sleek, modern look
17
- custom_css = \"\"\"
18
  .gradio-container { background-color: #0b0f19; font-family: 'Inter', sans-serif; }
19
  #title-header { text-align: center; margin-bottom: 20px; }
20
  #title-header h1 { color: #38bdf8; font-weight: 800; font-size: 2.2rem; }
21
  .sidebar-panel { background: #111827 !important; border: 1px solid #1f2937 !important; border-radius: 12px !important; }
22
  .chat-window { border: 1px solid #1f2937 !important; border-radius: 12px !important; background: #111827 !important; }
23
- \"\"\"
24
 
25
  def chat_stream(message, history, model, system_prompt, temperature, max_tokens):
26
- \"\"\"
27
  Custom chat function built for gr.Chatbot component format.
28
- history is a list of dicts or tuples depending on older/newer Gradio versions.
29
- \"\"\"
30
  # 1. Start with the custom system prompt
31
  messages = [{"role": "system", "content": system_prompt}]
32
 
33
  # 2. Append the conversation history
34
  for turn in history:
35
- # Check if history format is dict or list/tuple
36
- if isinstance(turn, dict):
37
- messages.append({"role": turn["role"], "content": turn["content"]})
38
- else:
39
- messages.append({"role": "user", "content": turn[0]})
40
- messages.append({"role": "assistant", "content": turn[1]})
41
 
42
  # 3. Append current user message
43
  messages.append({"role": "user", "content": message})
@@ -63,9 +53,9 @@ def chat_stream(message, history, model, system_prompt, temperature, max_tokens)
63
 
64
 
65
  # Building the Interactive Interface using Blocks
66
- with gr.Blocks() as demo:
67
 
68
- # Title Section
69
  gr.Markdown("# 🚀 Personal AI ChatBot", elem_id="title-header")
70
  gr.Markdown("A fully customizable, hyper-fast LLM workspace.")
71
 
@@ -105,7 +95,10 @@ with gr.Blocks() as demo:
105
 
106
  # --- RIGHT COLUMN: CHAT INTERFACE ---
107
  with gr.Column(scale=3):
108
- chatbot = gr.Chatbot(elem_classes="chat-window")
 
 
 
109
 
110
  # Textbox and setup for user interaction
111
  msg_input = gr.Textbox(
@@ -120,17 +113,9 @@ with gr.Blocks() as demo:
120
  chatbot=chatbot,
121
  textbox=msg_input,
122
  additional_inputs=[model_select, system_input, temp_slider, tokens_slider]
 
123
  )
124
 
125
  if __name__ == "__main__":
126
- demo.launch(css=custom_css, theme=gr.themes.Soft())
127
- """
128
-
129
- # Write files to disk
130
- with open("requirements.txt", "w", encoding="utf-8") as f:
131
- f.write(requirements_content.strip())
132
-
133
- with open("app.py", "w", encoding="utf-8") as f:
134
- f.write(app_content.strip())
135
-
136
- print("Files created successfully.")
 
1
+ import gradio as gr
 
 
 
2
  import os
3
  from groq import Groq
4
+ from google.colab import userdata
5
 
6
  # Initialize the Groq client
7
+ # Retrieve API key from Colab's userdata
8
+ os.environ["GROQ_API_KEY"] = userdata.get('ChatBot_API')
9
+ client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
 
 
 
10
 
11
  # Custom CSS to give it a sleek, modern look
12
+ custom_css = """
13
  .gradio-container { background-color: #0b0f19; font-family: 'Inter', sans-serif; }
14
  #title-header { text-align: center; margin-bottom: 20px; }
15
  #title-header h1 { color: #38bdf8; font-weight: 800; font-size: 2.2rem; }
16
  .sidebar-panel { background: #111827 !important; border: 1px solid #1f2937 !important; border-radius: 12px !important; }
17
  .chat-window { border: 1px solid #1f2937 !important; border-radius: 12px !important; background: #111827 !important; }
18
+ """
19
 
20
  def chat_stream(message, history, model, system_prompt, temperature, max_tokens):
21
+ """
22
  Custom chat function built for gr.Chatbot component format.
23
+ history is a list of dicts: [{"role": "user", "content": "..."}, ...]
24
+ """
25
  # 1. Start with the custom system prompt
26
  messages = [{"role": "system", "content": system_prompt}]
27
 
28
  # 2. Append the conversation history
29
  for turn in history:
30
+ messages.append({"role": turn["role"], "content": turn["content"]})
 
 
 
 
 
31
 
32
  # 3. Append current user message
33
  messages.append({"role": "user", "content": message})
 
53
 
54
 
55
  # Building the Interactive Interface using Blocks
56
+ with gr.Blocks() as demo: # Removed css and theme from Blocks constructor
57
 
58
+ # Title Section - Replaced gr.Div with gr.Markdown
59
  gr.Markdown("# 🚀 Personal AI ChatBot", elem_id="title-header")
60
  gr.Markdown("A fully customizable, hyper-fast LLM workspace.")
61
 
 
95
 
96
  # --- RIGHT COLUMN: CHAT INTERFACE ---
97
  with gr.Column(scale=3):
98
+ chatbot = gr.Chatbot(
99
+ elem_classes="chat-window"
100
+ # Removed bubble_full_width as it's not a valid argument for gr.Chatbot
101
+ )
102
 
103
  # Textbox and setup for user interaction
104
  msg_input = gr.Textbox(
 
113
  chatbot=chatbot,
114
  textbox=msg_input,
115
  additional_inputs=[model_select, system_input, temp_slider, tokens_slider]
116
+ # Removed type="messages" as it's not a valid argument for gr.ChatInterface
117
  )
118
 
119
  if __name__ == "__main__":
120
+ # Moved css and theme to launch()
121
+ demo.launch(css=custom_css, theme=gr.themes.Soft())