Solus-PG commited on
Commit
e3e9da5
·
verified ·
1 Parent(s): 2592ec1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -13
app.py CHANGED
@@ -18,8 +18,7 @@ if not os.path.exists(log_file):
18
  # Model information
19
  MODEL_ID = "Solus-PG/gemma-2b-gaslighting" # Replace with your actual model path
20
 
21
- # Load the model and tokenizer (with error handling)
22
- @gr.on_load(api_name="load_model")
23
  def load_model():
24
  print("Loading model...")
25
  try:
@@ -33,14 +32,21 @@ def load_model():
33
  return model, tokenizer
34
  except Exception as e:
35
  print(f"Error loading model: {e}")
36
- raise gr.Error(f"Failed to load model: {str(e)}")
37
 
38
  # Generate response function
39
- def generate_response(prompt, model_state):
40
- if not model_state:
41
- return "Model is not loaded yet. Please wait or refresh the page."
42
-
43
- model, tokenizer = model_state
 
 
 
 
 
 
 
44
 
45
  try:
46
  # Format as chat for the model
@@ -85,6 +91,9 @@ with gr.Blocks(css="footer {visibility: hidden}") as demo:
85
  **Note: The responses from this model should not be taken as truth.**
86
  """)
87
 
 
 
 
88
  with gr.Row():
89
  with gr.Column():
90
  input_text = gr.Textbox(
@@ -101,11 +110,6 @@ with gr.Blocks(css="footer {visibility: hidden}") as demo:
101
  label="AI Response"
102
  )
103
 
104
- model_state = gr.State()
105
-
106
- # Load model when the app starts
107
- demo.load(load_model, outputs=model_state)
108
-
109
  # Handle submission
110
  submit_btn.click(
111
  fn=generate_response,
 
18
  # Model information
19
  MODEL_ID = "Solus-PG/gemma-2b-gaslighting" # Replace with your actual model path
20
 
21
+ # Load the model and tokenizer
 
22
  def load_model():
23
  print("Loading model...")
24
  try:
 
32
  return model, tokenizer
33
  except Exception as e:
34
  print(f"Error loading model: {e}")
35
+ return None, None
36
 
37
  # Generate response function
38
+ def generate_response(prompt, model_state=None):
39
+ # If model state isn't passed, try loading the model
40
+ if model_state is None or model_state == [None, None]:
41
+ try:
42
+ model, tokenizer = load_model()
43
+ except Exception:
44
+ return "Failed to load the model. Please check logs or try again later."
45
+ else:
46
+ model, tokenizer = model_state
47
+
48
+ if model is None or tokenizer is None:
49
+ return "Model couldn't be loaded. Please check logs or try again later."
50
 
51
  try:
52
  # Format as chat for the model
 
91
  **Note: The responses from this model should not be taken as truth.**
92
  """)
93
 
94
+ # Load model at startup and store in session state
95
+ model_state = gr.State(value=load_model())
96
+
97
  with gr.Row():
98
  with gr.Column():
99
  input_text = gr.Textbox(
 
110
  label="AI Response"
111
  )
112
 
 
 
 
 
 
113
  # Handle submission
114
  submit_btn.click(
115
  fn=generate_response,