aayushraina commited on
Commit
69497aa
·
verified ·
1 Parent(s): bd1e246

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -1
app.py CHANGED
@@ -1 +1,55 @@
1
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import GPT2LMHeadModel, GPT2Tokenizer
4
+ import os
5
+ from huggingface_hub import login
6
+
7
+ # Authenticate with Hugging Face
8
+ def init_huggingface():
9
+ try:
10
+ # Get token from environment variable or use default
11
+ hf_token = os.getenv('HUGGINGFACE_TOKEN')
12
+ if hf_token:
13
+ login(hf_token)
14
+ print("Successfully logged in to Hugging Face")
15
+ else:
16
+ print("No Hugging Face token found, trying anonymous access")
17
+ except Exception as e:
18
+ print(f"Authentication error: {e}")
19
+
20
+ # Load model and tokenizer
21
+ def load_model():
22
+ try:
23
+ # Initialize Hugging Face authentication
24
+ init_huggingface()
25
+
26
+ print("Loading model...")
27
+ # Try loading with auth token first
28
+ model = GPT2LMHeadModel.from_pretrained(
29
+ "aayushraina/gpt2shakespeare",
30
+ local_files_only=False,
31
+ trust_remote_code=True
32
+ )
33
+ print("Model loaded successfully!")
34
+
35
+ print("Loading tokenizer...")
36
+ # Use the base GPT-2 tokenizer
37
+ tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
38
+ print("Tokenizer loaded successfully!")
39
+
40
+ model.eval()
41
+ return model, tokenizer
42
+ except Exception as e:
43
+ print(f"Error loading model or tokenizer: {e}")
44
+ try:
45
+ # Fallback to base GPT-2 if custom model fails
46
+ print("Attempting to load base GPT-2 model as fallback...")
47
+ model = GPT2LMHeadModel.from_pretrained("gpt2")
48
+ tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
49
+ print("Fallback successful - loaded base GPT-2")
50
+ return model, tokenizer
51
+ except Exception as e:
52
+ print(f"Fallback failed: {e}")
53
+ return None, None
54
+
55
+ # Rest of the code remains the same...