Gajendra5490 commited on
Commit
4d933ae
·
verified ·
1 Parent(s): 84f763d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +154 -40
app.py CHANGED
@@ -1,50 +1,164 @@
1
- import gradio as gr
2
- from transformers import AutoModelForCausalLM, AutoTokenizer
3
-
4
- # 1. Load your fine-tuned model
5
- model_path = "Gajendra5490/Scrached_Trained_Model" # Assuming model is in the same directory as app.py
6
- tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
7
- model = AutoModelForCausalLM.from_pretrained(
8
- model_path,
9
- device_map="auto",
10
- torch_dtype="auto",
11
- trust_remote_code=True
12
- )
 
 
 
13
 
14
- # 2. Create inference function
15
- def chat_with_model(user_input):
16
- prompt = f"### Instruction:\n{user_input}\n\n### Response:\n"
17
- inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
18
-
19
- output = model.generate(
20
- **inputs,
21
- max_new_tokens=300,
22
- temperature=0.7,
23
- top_p=0.95,
24
- do_sample=True,
25
- repetition_penalty=1.1
26
  )
27
- response = tokenizer.decode(output[0], skip_special_tokens=True)
28
- # Extract only the model's answer
29
- generated_text = response.split("### Response:")[-1].strip()
30
- return generated_text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
- # 3. Create Gradio Interface
33
  interface = gr.Interface(
34
- fn=chat_with_model,
35
  inputs=gr.Textbox(
36
- lines=2,
37
- placeholder="Ask your skincare question...",
38
- label="User Input"
39
  ),
40
  outputs=gr.Textbox(
41
- label="Aesthetic AI's Reply"
 
42
  ),
43
- title="🧴 Aesthetic AI - Skincare Assistant",
44
- description="Chat with Aesthetic AI for your skin concerns. Powered by fine-tuned Llama 3!",
45
- theme="default"
 
 
 
 
46
  )
47
 
48
- # 4. Launch App
49
- if __name__ == "__main__":
50
- interface.launch()
 
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__)
26
+
27
+ logger = setup_logging()
28
+
29
+ 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,
45
+ weights_only=False
46
+ )
47
+
48
+ # Load tokenizer
49
+ with open(tokenizer_path, 'r', encoding='utf-8') as f:
50
+ tokenizer_data = json.load(f)
51
+
52
+ # Initialize model
53
+ from model import ImprovedTransformer
54
+ model_config = model_data['model_config']
55
+
56
+ self.model = ImprovedTransformer(
57
+ vocab_size=len(tokenizer_data['vocab']),
58
+ d_model=model_config.get('d_model', 512),
59
+ nhead=model_config.get('nhead', 8),
60
+ num_encoder_layers=model_config.get('num_encoder_layers', 6),
61
+ num_decoder_layers=model_config.get('num_decoder_layers', 6),
62
+ dim_feedforward=model_config.get('dim_feedforward', 2048),
63
+ dropout=model_config.get('dropout', 0.1),
64
+ max_seq_length=model_config.get('max_seq_length', 128)
65
+ ).to(self.device)
66
+
67
+ self.model.load_state_dict(model_data['model_state_dict'])
68
+ self.model.eval()
69
+
70
+ # Initialize tokenizer
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}")
78
+ raise
79
+
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,
94
+ torch.tensor([response_ids]).to(self.device)
95
+ )
96
+ next_token = curr_output[0, -1].argmax().item()
97
+
98
+ if next_token == self.tokenizer.special_tokens["<eos>"]:
99
+ break
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)}"
141
 
142
+ # Create Gradio interface
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
164
+ interface.launch()