Gajendra5490 commited on
Commit
82a3a32
·
verified ·
1 Parent(s): 6c20e09

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -43
app.py CHANGED
@@ -1,25 +1,21 @@
1
- try:
2
- import torch
3
- import gradio as gr
4
- import json
5
- import logging
6
- from pathlib import Path
7
- except ImportError as e:
8
- print(f"Missing required package: {e}")
9
- print("Please install required packages using:")
10
- print("pip install torch gradio")
11
- exit(1)
12
 
13
  # Configuration constants
 
14
  CURRENT_USER = "gajendra82"
15
- CURRENT_UTC = "2025-05-06 15:10:08"
16
 
17
  def setup_logging():
18
  logging.basicConfig(
19
  level=logging.INFO,
20
  format='%(asctime)s - %(levelname)s - %(message)s',
21
  handlers=[
22
- logging.StreamHandler() # Only console logging for Hugging Face
23
  ]
24
  )
25
  return logging.getLogger(__name__)
@@ -30,15 +26,26 @@ class ModelInference:
30
  def __init__(self):
31
  self.logger = logging.getLogger(__name__)
32
  self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
 
33
  self.load_model()
34
-
35
  def load_model(self):
36
  try:
37
- # Load model - using local path in Hugging Face space
38
- model_path = Path('model.pt')
39
- tokenizer_path = Path('tokenizer.json')
 
 
 
 
40
 
41
- # Load model with weights_only=False
 
 
 
 
 
 
42
  model_data = torch.load(
43
  model_path,
44
  map_location=self.device,
@@ -46,6 +53,7 @@ class ModelInference:
46
  )
47
 
48
  # Load tokenizer
 
49
  with open(tokenizer_path, 'r', encoding='utf-8') as f:
50
  tokenizer_data = json.load(f)
51
 
@@ -71,7 +79,7 @@ class ModelInference:
71
  from tokenizer import EnhancedTokenizer
72
  self.tokenizer = EnhancedTokenizer(tokenizer_data['vocab'])
73
 
74
- self.logger.info("Model loaded successfully")
75
 
76
  except Exception as e:
77
  self.logger.error(f"Error loading model: {e}")
@@ -80,14 +88,18 @@ class ModelInference:
80
  @torch.no_grad()
81
  def generate_answer(self, input_text: str) -> str:
82
  try:
 
 
 
 
83
  # Tokenize input
84
  input_ids = self.tokenizer.encode(f"<user> {input_text} <sep>")
85
  input_tensor = torch.tensor([input_ids]).to(self.device)
86
 
87
- # Initialize response with start token
88
  response_ids = [self.tokenizer.special_tokens["<assistant>"]]
89
 
90
- # Generate output
91
  for _ in range(150): # max length
92
  curr_output = self.model(
93
  input_tensor,
@@ -100,41 +112,32 @@ class ModelInference:
100
 
101
  response_ids.append(next_token)
102
 
103
- # Decode output
104
  answer = self.tokenizer.decode(response_ids)
105
  answer = answer.replace("<assistant>", "").replace("<eos>", "").strip()
106
 
 
107
  return answer
108
 
109
  except Exception as e:
110
  self.logger.error(f"Error generating answer: {e}")
111
- return f"Error generating answer: {str(e)}"
112
 
113
- # Initialize model globally for Hugging Face
114
  try:
115
- print("Loading model...")
116
  model = ModelInference()
117
- print("Model loaded successfully")
118
  except Exception as e:
119
- print(f"Error loading model: {e}")
120
  model = None
121
 
122
  def process_input(input_text):
123
  """Process input through Gradio"""
124
  try:
125
  if model is None:
126
- return "Error: Model not loaded properly"
127
-
128
- # Log the input
129
- logger.info(f"Input received: {input_text}")
130
-
131
- # Generate answer
132
- answer = model.generate_answer(input_text)
133
-
134
- # Log the output
135
- logger.info(f"Generated answer: {answer}")
136
-
137
- return answer
138
  except Exception as e:
139
  logger.error(f"Error processing input: {e}")
140
  return f"Error: {str(e)}"
@@ -143,21 +146,29 @@ def process_input(input_text):
143
  interface = gr.Interface(
144
  fn=process_input,
145
  inputs=gr.Textbox(
146
- label="Input",
147
- placeholder="Enter your input here...",
148
  lines=2
149
  ),
150
  outputs=gr.Textbox(
151
- label="Answer",
152
  lines=4
153
  ),
154
  title="Model Inference Interface",
155
  description=f"""
 
156
  Current User: {CURRENT_USER}
157
  Last Updated: {CURRENT_UTC} UTC
 
 
158
  """,
159
  theme=gr.themes.Soft(),
160
- allow_flagging="never"
 
 
 
 
 
161
  )
162
 
163
  # For Hugging Face Spaces
 
1
+ import torch
2
+ import gradio as gr
3
+ import json
4
+ import logging
5
+ from pathlib import Path
6
+ from huggingface_hub import hf_hub_download
 
 
 
 
 
7
 
8
  # Configuration constants
9
+ MODEL_REPO = "Gajendra5490/Scrached_Trained_Model"
10
  CURRENT_USER = "gajendra82"
11
+ CURRENT_UTC = "2025-05-06 15:15:08"
12
 
13
  def setup_logging():
14
  logging.basicConfig(
15
  level=logging.INFO,
16
  format='%(asctime)s - %(levelname)s - %(message)s',
17
  handlers=[
18
+ logging.StreamHandler()
19
  ]
20
  )
21
  return logging.getLogger(__name__)
 
26
  def __init__(self):
27
  self.logger = logging.getLogger(__name__)
28
  self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
29
+ self.logger.info(f"Using device: {self.device}")
30
  self.load_model()
31
+
32
  def load_model(self):
33
  try:
34
+ # Download model and tokenizer from your Hugging Face repository
35
+ self.logger.info(f"Downloading model from {MODEL_REPO}")
36
+
37
+ model_path = hf_hub_download(
38
+ repo_id=MODEL_REPO,
39
+ filename="model.pt"
40
+ )
41
 
42
+ tokenizer_path = hf_hub_download(
43
+ repo_id=MODEL_REPO,
44
+ filename="tokenizer.json"
45
+ )
46
+
47
+ # Load model
48
+ self.logger.info("Loading model...")
49
  model_data = torch.load(
50
  model_path,
51
  map_location=self.device,
 
53
  )
54
 
55
  # Load tokenizer
56
+ self.logger.info("Loading tokenizer...")
57
  with open(tokenizer_path, 'r', encoding='utf-8') as f:
58
  tokenizer_data = json.load(f)
59
 
 
79
  from tokenizer import EnhancedTokenizer
80
  self.tokenizer = EnhancedTokenizer(tokenizer_data['vocab'])
81
 
82
+ self.logger.info("Model and tokenizer loaded successfully")
83
 
84
  except Exception as e:
85
  self.logger.error(f"Error loading model: {e}")
 
88
  @torch.no_grad()
89
  def generate_answer(self, input_text: str) -> str:
90
  try:
91
+ # Clean input
92
+ input_text = input_text.strip()
93
+ self.logger.info(f"Processing input: {input_text}")
94
+
95
  # Tokenize input
96
  input_ids = self.tokenizer.encode(f"<user> {input_text} <sep>")
97
  input_tensor = torch.tensor([input_ids]).to(self.device)
98
 
99
+ # Initialize response
100
  response_ids = [self.tokenizer.special_tokens["<assistant>"]]
101
 
102
+ # Generate response
103
  for _ in range(150): # max length
104
  curr_output = self.model(
105
  input_tensor,
 
112
 
113
  response_ids.append(next_token)
114
 
115
+ # Decode response
116
  answer = self.tokenizer.decode(response_ids)
117
  answer = answer.replace("<assistant>", "").replace("<eos>", "").strip()
118
 
119
+ self.logger.info(f"Generated response: {answer}")
120
  return answer
121
 
122
  except Exception as e:
123
  self.logger.error(f"Error generating answer: {e}")
124
+ return f"Error: {str(e)}"
125
 
126
+ # Initialize model
127
  try:
128
+ print("Initializing model...")
129
  model = ModelInference()
130
+ print("Model initialized successfully")
131
  except Exception as e:
132
+ print(f"Error initializing model: {e}")
133
  model = None
134
 
135
  def process_input(input_text):
136
  """Process input through Gradio"""
137
  try:
138
  if model is None:
139
+ return "Error: Model not initialized properly"
140
+ return model.generate_answer(input_text)
 
 
 
 
 
 
 
 
 
 
141
  except Exception as e:
142
  logger.error(f"Error processing input: {e}")
143
  return f"Error: {str(e)}"
 
146
  interface = gr.Interface(
147
  fn=process_input,
148
  inputs=gr.Textbox(
149
+ label="Input Question",
150
+ placeholder="Enter your question here...",
151
  lines=2
152
  ),
153
  outputs=gr.Textbox(
154
+ label="Model Response",
155
  lines=4
156
  ),
157
  title="Model Inference Interface",
158
  description=f"""
159
+ Model Repository: {MODEL_REPO}
160
  Current User: {CURRENT_USER}
161
  Last Updated: {CURRENT_UTC} UTC
162
+
163
+ Enter your question and click submit to get a response.
164
  """,
165
  theme=gr.themes.Soft(),
166
+ allow_flagging="never",
167
+ examples=[
168
+ ["What is this about?"],
169
+ ["Can you explain the topic?"],
170
+ ["Give me more details."]
171
+ ]
172
  )
173
 
174
  # For Hugging Face Spaces