yogies commited on
Commit
8d8b639
·
verified ·
1 Parent(s): 8316860

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +201 -44
app.py CHANGED
@@ -1,7 +1,70 @@
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
 
 
 
 
 
 
5
  def respond(
6
  message,
7
  history: list[dict[str, str]],
@@ -9,62 +72,156 @@ def respond(
9
  max_tokens,
10
  temperature,
11
  top_p,
12
- hf_token: gr.OAuthToken,
13
  ):
14
  """
15
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
16
  """
17
- client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
- messages = [{"role": "system", "content": system_message}]
 
 
 
20
 
21
- messages.extend(history)
 
 
 
 
 
 
 
 
22
 
23
- messages.append({"role": "user", "content": message})
 
 
 
 
 
 
 
 
 
 
24
 
25
- response = ""
 
 
 
 
 
26
 
27
- for message in client.chat_completion(
28
- messages,
29
- max_tokens=max_tokens,
30
- stream=True,
31
- temperature=temperature,
32
- top_p=top_p,
33
- ):
34
- choices = message.choices
35
- token = ""
36
- if len(choices) and choices[0].delta.content:
37
- token = choices[0].delta.content
38
 
39
- response += token
40
- yield response
41
 
 
 
 
 
42
 
43
  """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- chatbot = gr.ChatInterface(
47
- respond,
48
- type="messages",
49
- additional_inputs=[
50
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
51
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
52
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
53
- gr.Slider(
54
- minimum=0.1,
55
- maximum=1.0,
56
- value=0.95,
57
- step=0.05,
58
- label="Top-p (nucleus sampling)",
59
- ),
60
- ],
61
- )
62
-
63
- with gr.Blocks() as demo:
64
  with gr.Sidebar():
65
- gr.LoginButton()
66
- chatbot.render()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
 
 
 
 
69
  if __name__ == "__main__":
70
- demo.launch()
 
 
 
 
 
 
 
 
1
+
2
  import gradio as gr
3
+ import os
4
+ import json
5
+ from datetime import datetime, date
6
+ from openai import OpenAI
7
+
8
+ # ----------------------------------------------------------------------
9
+ # Helper to read secrets from the HF Space environment
10
+ # ----------------------------------------------------------------------
11
+ def _secret(key: str, fallback: str = None) -> str:
12
+ val = os.getenv(key)
13
+ if val is not None:
14
+ return val
15
+ if fallback is not None:
16
+ return fallback
17
+ raise RuntimeError(f"Secret '{key}' not found. Please add it to your Space secrets.")
18
+
19
+ # ----------------------------------------------------------------------
20
+ # User Management
21
+ # ----------------------------------------------------------------------
22
+ def load_users():
23
+ """Load users from secrets or environment variables"""
24
+ users = {}
25
+
26
+ # Try to load from JSON string
27
+ users_json = _secret("CHAT_USERS", "{}")
28
+ try:
29
+ users_data = json.loads(users_json)
30
+ for username, password in users_data.items():
31
+ users[username] = password
32
+ except:
33
+ pass
34
+
35
+
36
+ return users
37
+
38
+ # Load users
39
+ VALID_USERS = load_users()
40
+
41
+ def authenticate_user(username, password):
42
+ """Authenticate user against the valid users dictionary"""
43
+ return username in VALID_USERS and VALID_USERS[username] == password
44
 
45
+ # ----------------------------------------------------------------------
46
+ # Configuration
47
+ # ----------------------------------------------------------------------
48
+ # Available models with their respective API configurations
49
+ MODELS = {
50
+ "GPT-OSS-20B (Hugging Face)": {
51
+ "provider": "huggingface",
52
+ "model_name": "openai/gpt-oss-20b:nscale",
53
+ "api_url": "https://router.huggingface.co/v1"
54
+ },
55
+ "Nvidia": {
56
+ "provider": "openrouter",
57
+ "model_name": "nvidia/nemotron-nano-9b-v2:free",
58
+ "api_url": "https://openrouter.ai/api/v1"
59
+ }
60
+ }
61
 
62
+ # Get model display names for dropdown
63
+ MODEL_NAMES = list(MODELS.keys())
64
+
65
+ # ----------------------------------------------------------------------
66
+ # Core Chat Logic
67
+ # ----------------------------------------------------------------------
68
  def respond(
69
  message,
70
  history: list[dict[str, str]],
 
72
  max_tokens,
73
  temperature,
74
  top_p,
75
+ selected_model,
76
  ):
77
  """
78
+ Handle chat responses using the selected model
79
  """
80
+ try:
81
+ # Check expiration (optional - remove if not needed)
82
+ # end_date = datetime.strptime(_secret("END_DATE", "2026-12-31"), "%Y-%m-%d").date()
83
+ # if date.today() > end_date:
84
+ # yield "Chatbot has expired."
85
+ # return
86
+
87
+ # Get model configuration
88
+ model_config = MODELS[selected_model]
89
+ provider = model_config["provider"]
90
+
91
+ # Get API key based on provider
92
+ if provider == "huggingface":
93
+ api_key = _secret("HF_TOKEN")
94
+ else: # openrouter
95
+ api_key = _secret("OPENROUTER_KEY")
96
+
97
+ # Configure client
98
+ client = OpenAI(
99
+ base_url=model_config["api_url"],
100
+ api_key=api_key
101
+ )
102
 
103
+ # Prepare messages
104
+ messages = [{"role": "system", "content": system_message}]
105
+ messages.extend(history)
106
+ messages.append({"role": "user", "content": message})
107
 
108
+ # Make the API call
109
+ response = client.chat.completions.create(
110
+ model=model_config["model_name"],
111
+ messages=messages,
112
+ max_tokens=max_tokens,
113
+ temperature=temperature,
114
+ top_p=top_p,
115
+ stream=True,
116
+ )
117
 
118
+ # Stream the response
119
+ full_response = ""
120
+ for chunk in response:
121
+ if chunk.choices[0].delta.content is not None:
122
+ token = chunk.choices[0].delta.content
123
+ full_response += token
124
+ yield full_response
125
+
126
+ except Exception as e:
127
+ print(f"Error in respond function: {e}")
128
+ yield f"Error: {str(e)}"
129
 
130
+ # ----------------------------------------------------------------------
131
+ # Custom Auth Function for Gradio
132
+ # ----------------------------------------------------------------------
133
+ def gradio_auth(username, password):
134
+ """Custom authentication function for Gradio"""
135
+ return authenticate_user(username, password)
136
 
137
+ # ----------------------------------------------------------------------
138
+ # UI Layout
139
+ # ----------------------------------------------------------------------
140
+ # Tips section
141
+ tips_md = """
 
 
 
 
 
 
142
 
143
+ """
 
144
 
145
+ # Footer
146
+ footer_md = """
147
+ ---
148
+ **Providers**: Hugging Face Inference API + OpenRouter, dipilih providers yang tidak menggunakan prompt untuk training data.
149
 
150
  """
151
+
152
+ # Create the chat interface
153
+ with gr.Blocks(
154
+ title="Multi-Model Chat Interface",
155
+ theme=gr.themes.Soft()
156
+ ) as demo:
157
+
158
+ gr.Markdown("# AI Chat")
159
+ gr.Markdown("Model yang digunakan gratis. Data tidak digunakan untuk training.")
160
+
161
+ # Model selection and settings in sidebar
 
 
 
 
 
 
 
 
 
162
  with gr.Sidebar():
163
+ gr.Markdown("### ⚙️ Configuration")
164
+
165
+ # Model selection
166
+ selected_model = gr.Dropdown(
167
+ choices=MODEL_NAMES,
168
+ value=MODEL_NAMES[0],
169
+ label="Select Model",
170
+ info="Choose which AI model to use"
171
+ )
172
+
173
+ # Display current user (if available)
174
+ current_user = gr.Textbox(
175
+ label="Current User",
176
+ value="Authenticated User",
177
+ interactive=False,
178
+ visible=False # Hide by default, can set to True if you want to show
179
+ )
180
+
181
+ # Advanced settings
182
+ with gr.Accordion("Advanced Settings", open=False):
183
+ system_message = gr.Textbox(
184
+ value="Respon dalam Bahasa Indonesia.",
185
+ label="System Message",
186
+ info="Controls the AI's behavior and personality"
187
+ )
188
+ max_tokens = gr.Slider(
189
+ minimum=1, maximum=8096, value=4096, step=1,
190
+ label="Max New Tokens",
191
+ info="Jumlah token respon maksimum."
192
+ )
193
 
194
+
195
+ # Main chat interface
196
+ chatbot = gr.ChatInterface(
197
+ respond,
198
+ type="messages",
199
+ additional_inputs=[
200
+ system_message,
201
+ max_tokens,
202
+ selected_model,
203
+ ],
204
+ examples=[
205
+ [""],
206
+ ["Explain quantum computing in simple terms"],
207
+ ["What are the advantages of using open-source AI models?"]
208
+ ],
209
+ cache_examples=False,
210
+ )
211
+
212
+ # Tips and footer
213
+ gr.Markdown(tips_md)
214
+ gr.Markdown(footer_md)
215
 
216
+ # ----------------------------------------------------------------------
217
+ # Launch with Custom Auth
218
+ # ----------------------------------------------------------------------
219
  if __name__ == "__main__":
220
+ demo.launch(
221
+ auth=gradio_auth, # Use our custom auth function
222
+ auth_message="Please login to access the chat interface",
223
+ server_name="0.0.0.0",
224
+ ssr_mode=False,
225
+ server_port=7860,
226
+ show_error=True
227
+ )