Meshyboi commited on
Commit
f005dbe
·
verified ·
1 Parent(s): 361e35b

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -6
app.py CHANGED
@@ -2,14 +2,14 @@ from fastapi import FastAPI, HTTPException
2
  from pydantic import BaseModel
3
  from typing import Dict, List
4
  import os
5
- import keras
6
  import numpy as np
7
- from transformers import RobertaTokenizer
8
  from huggingface_hub import hf_hub_download
9
  import uvicorn
10
 
11
- # Set Keras backend
12
- os.environ["KERAS_BACKEND"] = "tensorflow"
13
 
14
  # Initialize FastAPI app
15
  app = FastAPI(
@@ -56,8 +56,10 @@ def load_model():
56
  cache_dir=None # Use default cache
57
  )
58
  print(f"Model downloaded to: {model_path}")
59
- # Load the model from the downloaded file
60
- model = keras.saving.load_model(model_path)
 
 
61
  return model
62
  except Exception as e:
63
  raise RuntimeError(f"Error loading model: {str(e)}")
 
2
  from pydantic import BaseModel
3
  from typing import Dict, List
4
  import os
5
+ import tensorflow as tf
6
  import numpy as np
7
+ from transformers import RobertaTokenizer, TFRobertaModel
8
  from huggingface_hub import hf_hub_download
9
  import uvicorn
10
 
11
+ # Ensure TensorFlow uses its own Keras
12
+ os.environ["TF_USE_LEGACY_KERAS"] = "1"
13
 
14
  # Initialize FastAPI app
15
  app = FastAPI(
 
56
  cache_dir=None # Use default cache
57
  )
58
  print(f"Model downloaded to: {model_path}")
59
+ # Load the model using TensorFlow's Keras (not standalone Keras)
60
+ # This is needed because the model was saved with tf_keras
61
+ model = tf.keras.models.load_model(model_path, compile=False)
62
+ print("Model loaded successfully!")
63
  return model
64
  except Exception as e:
65
  raise RuntimeError(f"Error loading model: {str(e)}")