eyosi1 commited on
Commit
7e6100e
·
verified ·
1 Parent(s): 9f4b1c6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -21
app.py CHANGED
@@ -1,10 +1,33 @@
1
  import gradio as gr
 
2
 
3
- # Function to handle model inference with config
4
- def generate_text(prompt, temperature, max_tokens):
5
- # Simulate model inference with config (replace with actual model call)
6
- response = f"Response to '{prompt}' with temperature={temperature} and max_tokens={max_tokens}"
7
- return response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- # Model configuration
19
- gr.Markdown("### Model Configuration")
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 (example logic)
33
- def handle_login(token):
34
- if token: # Replace with actual authentication logic
35
- return "Logged in successfully!"
 
36
  else:
37
- return "Please enter a valid token."
38
 
39
- # Handle text generation
40
- generate_button.click(generate_text, [prompt, temperature, max_tokens], output)
41
 
42
- # Handle login
43
- login_button.click(handle_login, inputs=token_input, outputs=login_status)
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()