Abhlash commited on
Commit
9457549
·
verified ·
1 Parent(s): 30af1b3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -2
app.py CHANGED
@@ -1,9 +1,36 @@
1
  import gradio as gr
2
  from transformers import AutoTokenizer, AutoModelForCausalLM
3
- from huggingface_hub import login
 
 
 
 
 
 
4
 
5
  # Login to Hugging Face (you'll need to set HUGGINGFACE_TOKEN in your Secrets)
6
- login()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
  # Load model and tokenizer
9
  tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.1-70B")
 
1
  import gradio as gr
2
  from transformers import AutoTokenizer, AutoModelForCausalLM
3
+ from huggingface_hub import login, HfApi
4
+ import os
5
+ from dotenv import load_dotenv
6
+ from flask import flash, redirect, url_for, session
7
+
8
+ # Load environment variables
9
+ load_dotenv()
10
 
11
  # Login to Hugging Face (you'll need to set HUGGINGFACE_TOKEN in your Secrets)
12
+ def login():
13
+ # Get the Hugging Face token from environment variable
14
+ hf_token = os.getenv('HUGGING_FACE_TOKEN')
15
+
16
+ if not hf_token:
17
+ flash('Hugging Face token not found. Please set the HUGGING_FACE_TOKEN environment variable.', 'error')
18
+ return redirect(url_for('index'))
19
+
20
+ # Use the token for authentication
21
+ try:
22
+ api = HfApi()
23
+ user = api.whoami(token=hf_token)
24
+ if user:
25
+ session['logged_in'] = True
26
+ flash('Successfully authenticated with Hugging Face', 'success')
27
+ else:
28
+ session['logged_in'] = False
29
+ flash('Failed to authenticate with Hugging Face', 'error')
30
+ except Exception as e:
31
+ session['logged_in'] = False
32
+ flash(f'Error during Hugging Face authentication: {str(e)}', 'error')
33
+ return redirect(url_for('index'))
34
 
35
  # Load model and tokenizer
36
  tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.1-70B")