Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,33 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
|
3 |
-
# Function to handle
|
4 |
-
def
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
# Gradio interface
|
10 |
with gr.Blocks() as demo:
|
@@ -15,13 +38,8 @@ with gr.Blocks() as demo:
|
|
15 |
login_button = gr.Button("Sign in")
|
16 |
login_status = gr.Markdown("")
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
temperature = gr.Slider(0.1, 1.0, value=0.7, label="Temperature")
|
21 |
-
max_tokens = gr.Slider(10, 500, value=100, label="Max Tokens")
|
22 |
-
|
23 |
-
# Input and output components
|
24 |
-
with gr.Column():
|
25 |
prompt = gr.Textbox(label="Your Prompt")
|
26 |
output = gr.Textbox(label="Model Response")
|
27 |
generate_button = gr.Button("Generate")
|
@@ -29,18 +47,18 @@ with gr.Blocks() as demo:
|
|
29 |
# Load the model
|
30 |
model_interface = gr.load("models/deepseek-ai/DeepSeek-Coder-V2-Lite-Instruct", provider="nebius")
|
31 |
|
32 |
-
# Handle login
|
33 |
-
def
|
34 |
-
|
35 |
-
|
|
|
36 |
else:
|
37 |
-
return
|
38 |
|
39 |
-
|
40 |
-
generate_button.click(generate_text, [prompt, temperature, max_tokens], output)
|
41 |
|
42 |
-
# Handle
|
43 |
-
|
44 |
|
45 |
# Launch the app
|
46 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from huggingface_hub import login, logout, whoami
|
3 |
|
4 |
+
# Function to handle login
|
5 |
+
def handle_login(token):
|
6 |
+
try:
|
7 |
+
# Attempt to log in with the provided token
|
8 |
+
login(token=token, add_to_git_credential=False)
|
9 |
+
user_info = whoami()
|
10 |
+
return f"Logged in as: {user_info['name']}"
|
11 |
+
except Exception as e:
|
12 |
+
# Handle login failure
|
13 |
+
logout() # Ensure the user is logged out if login fails
|
14 |
+
return f"Login failed: {str(e)}"
|
15 |
+
|
16 |
+
# Function to check if the user is logged in
|
17 |
+
def is_logged_in():
|
18 |
+
try:
|
19 |
+
# Check if the user is authenticated
|
20 |
+
whoami()
|
21 |
+
return True
|
22 |
+
except:
|
23 |
+
return False
|
24 |
+
|
25 |
+
# Function to restrict access to the app
|
26 |
+
def restricted_functionality(prompt):
|
27 |
+
if not is_logged_in():
|
28 |
+
return "Please log in to use this feature."
|
29 |
+
# Simulate model response (replace with actual model inference)
|
30 |
+
return f"Model response to: {prompt}"
|
31 |
|
32 |
# Gradio interface
|
33 |
with gr.Blocks() as demo:
|
|
|
38 |
login_button = gr.Button("Sign in")
|
39 |
login_status = gr.Markdown("")
|
40 |
|
41 |
+
# Main app functionality
|
42 |
+
with gr.Column(visible=False) as main_interface: # Hide until logged in
|
|
|
|
|
|
|
|
|
|
|
43 |
prompt = gr.Textbox(label="Your Prompt")
|
44 |
output = gr.Textbox(label="Model Response")
|
45 |
generate_button = gr.Button("Generate")
|
|
|
47 |
# Load the model
|
48 |
model_interface = gr.load("models/deepseek-ai/DeepSeek-Coder-V2-Lite-Instruct", provider="nebius")
|
49 |
|
50 |
+
# Handle login
|
51 |
+
def update_interface(token):
|
52 |
+
login_result = handle_login(token)
|
53 |
+
if "Logged in as:" in login_result:
|
54 |
+
return {main_interface: gr.update(visible=True), login_status: login_result}
|
55 |
else:
|
56 |
+
return {main_interface: gr.update(visible=False), login_status: login_result}
|
57 |
|
58 |
+
login_button.click(update_interface, inputs=token_input, outputs=[main_interface, login_status])
|
|
|
59 |
|
60 |
+
# Handle text generation (restricted to logged-in users)
|
61 |
+
generate_button.click(restricted_functionality, inputs=prompt, outputs=output)
|
62 |
|
63 |
# Launch the app
|
64 |
demo.launch()
|