Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,3 @@
|
|
| 1 |
-
# !pip install flask-cors
|
| 2 |
-
# !pip install Flask pyngrok requests cloudinary SpeechRecognition pydub happytransformer transformers torch faiss-cpu sentence-transformers pandas unsloth bitsandbytes webrtcvad
|
| 3 |
-
# !ngrok config add-authtoken 2nFD4jJkAN642UzGI86nDsSC4qs_2cDEGBUFVpbQ5KaDuu4ys
|
| 4 |
import os
|
| 5 |
import faiss
|
| 6 |
import torch
|
|
@@ -8,6 +5,7 @@ import pandas as pd
|
|
| 8 |
from sentence_transformers import SentenceTransformer
|
| 9 |
from flask import Flask, request, jsonify, render_template
|
| 10 |
from flask_cors import CORS
|
|
|
|
| 11 |
import requests
|
| 12 |
import cloudinary
|
| 13 |
import cloudinary.uploader
|
|
@@ -27,51 +25,39 @@ from werkzeug.utils import secure_filename
|
|
| 27 |
from geopy.geocoders import Nominatim
|
| 28 |
import pickle
|
| 29 |
import numpy as np
|
| 30 |
-
from huggingface_hub import snapshot_download
|
| 31 |
|
| 32 |
# Configure logging
|
| 33 |
logging.basicConfig(level=logging.INFO)
|
| 34 |
|
| 35 |
-
# Initialize Flask app
|
| 36 |
-
app = Flask(__name__, template_folder="templates")
|
| 37 |
-
CORS(app)
|
| 38 |
-
|
| 39 |
# Load environment variables
|
| 40 |
-
API_KEY = os.getenv("
|
| 41 |
-
CSE_ID = os.getenv("
|
| 42 |
-
CLOUDINARY_CLOUD_NAME = os.getenv("
|
| 43 |
-
CLOUDINARY_API_KEY = os.getenv("
|
| 44 |
-
CLOUDINARY_API_SECRET = os.getenv("
|
| 45 |
-
|
| 46 |
-
# Define paths
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
# Check device
|
| 53 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 54 |
print(f"Using device: {device}")
|
| 55 |
|
| 56 |
-
# Initialize conversation context
|
| 57 |
-
conversation_context = {}
|
| 58 |
-
|
| 59 |
# Load SentenceTransformer model
|
| 60 |
def load_sentence_transformer():
|
| 61 |
print("Loading SentenceTransformer model...")
|
| 62 |
try:
|
| 63 |
-
|
| 64 |
-
os.environ['TRUST_REMOTE_CODE'] = "1"
|
| 65 |
-
|
| 66 |
-
from sentence_transformers import SentenceTransformer
|
| 67 |
-
model_embedding = SentenceTransformer(
|
| 68 |
-
'jinaai/jina-embeddings-v3',
|
| 69 |
-
trust_remote_code=True
|
| 70 |
-
).to(device)
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
state_dict = torch.load(MODEL_PATH, map_location=device)
|
| 75 |
|
| 76 |
# Dequantize if needed
|
| 77 |
for key, tensor in state_dict.items():
|
|
@@ -90,18 +76,14 @@ def load_sentence_transformer():
|
|
| 90 |
# Load FAISS index
|
| 91 |
def load_faiss_index():
|
| 92 |
print("Loading FAISS index...")
|
| 93 |
-
|
| 94 |
-
raise FileNotFoundError(f"FAISS index not found at {FAISS_INDEX_PATH}. Please ensure the file exists.")
|
| 95 |
-
index = faiss.read_index(FAISS_INDEX_PATH)
|
| 96 |
print("FAISS index loaded successfully.")
|
| 97 |
return index
|
| 98 |
|
| 99 |
# Load dataset
|
| 100 |
def load_dataset():
|
| 101 |
print("Loading dataset...")
|
| 102 |
-
|
| 103 |
-
raise FileNotFoundError(f"Dataset file not found at {DATASET_PATH}. Please ensure the file exists.")
|
| 104 |
-
df = pd.read_csv(DATASET_PATH)
|
| 105 |
print("Dataset loaded successfully.")
|
| 106 |
return df
|
| 107 |
|
|
@@ -112,7 +94,7 @@ class CustomRagRetriever:
|
|
| 112 |
self.model = model
|
| 113 |
self.pca = None
|
| 114 |
# Load PCA if it exists
|
| 115 |
-
pca_path = os.path.join(os.path.dirname(
|
| 116 |
if os.path.exists(pca_path):
|
| 117 |
with open(pca_path, 'rb') as f:
|
| 118 |
self.pca = pickle.load(f)
|
|
@@ -159,11 +141,11 @@ retriever = CustomRagRetriever(index, model_embedding)
|
|
| 159 |
# Load tokenizer and LLM model
|
| 160 |
def load_tokenizer_and_model():
|
| 161 |
print("Loading tokenizer...")
|
| 162 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 163 |
print("Tokenizer loaded successfully.")
|
| 164 |
|
| 165 |
print("Loading LLM model...")
|
| 166 |
-
model_llm = AutoModelForCausalLM.from_pretrained(
|
| 167 |
print("LLM model loaded successfully.")
|
| 168 |
return tokenizer, model_llm
|
| 169 |
|
|
@@ -381,8 +363,30 @@ def transcribe_with_vad(audio_file):
|
|
| 381 |
print(f"Could not request results from Google Speech Recognition service; {e}")
|
| 382 |
return ""
|
| 383 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 384 |
@app.route('/')
|
| 385 |
def index():
|
|
|
|
| 386 |
return render_template('index.html')
|
| 387 |
|
| 388 |
@app.route('/search', methods=['POST'])
|
|
@@ -642,5 +646,9 @@ def set_location():
|
|
| 642 |
return jsonify({"error": f"Error processing location: {str(e)}"}), 500
|
| 643 |
|
| 644 |
if __name__ == '__main__':
|
| 645 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 646 |
app.run(host='0.0.0.0', port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import faiss
|
| 3 |
import torch
|
|
|
|
| 5 |
from sentence_transformers import SentenceTransformer
|
| 6 |
from flask import Flask, request, jsonify, render_template
|
| 7 |
from flask_cors import CORS
|
| 8 |
+
from pyngrok import ngrok
|
| 9 |
import requests
|
| 10 |
import cloudinary
|
| 11 |
import cloudinary.uploader
|
|
|
|
| 25 |
from geopy.geocoders import Nominatim
|
| 26 |
import pickle
|
| 27 |
import numpy as np
|
|
|
|
| 28 |
|
| 29 |
# Configure logging
|
| 30 |
logging.basicConfig(level=logging.INFO)
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
# Load environment variables
|
| 33 |
+
API_KEY = os.getenv("AIzaSyC5FSchUVhKWetUIYPMe92B_1oRqhGplqI")
|
| 34 |
+
CSE_ID = os.getenv("c03c5384c2c5d424b")
|
| 35 |
+
CLOUDINARY_CLOUD_NAME = os.getenv("dn4rackei")
|
| 36 |
+
CLOUDINARY_API_KEY = os.getenv("599266248716888")
|
| 37 |
+
CLOUDINARY_API_SECRET = os.getenv("DRAaasqskCvfAhJhcKB6AKxrD7U")
|
| 38 |
+
|
| 39 |
+
# Define paths
|
| 40 |
+
load_dir = "./models/new_rag_model/"
|
| 41 |
+
model_path = os.path.join(load_dir, "model_state_dict.pth")
|
| 42 |
+
faiss_index_path = os.path.join(load_dir, "property_faiss.index")
|
| 43 |
+
dataset_path = os.path.join(load_dir, "property_data.csv")
|
| 44 |
+
model_dir = "./models/llm_model"
|
| 45 |
+
# model_dir = "/content/drive/MyDrive/newllmmodel/final_model"
|
| 46 |
+
# model_dir = "/content/drive/MyDrive/real_estate_model/final_model"
|
| 47 |
+
# model_dir = "/content/drive/MyDrive/rag"
|
| 48 |
|
| 49 |
# Check device
|
| 50 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 51 |
print(f"Using device: {device}")
|
| 52 |
|
|
|
|
|
|
|
|
|
|
| 53 |
# Load SentenceTransformer model
|
| 54 |
def load_sentence_transformer():
|
| 55 |
print("Loading SentenceTransformer model...")
|
| 56 |
try:
|
| 57 |
+
model_embedding = SentenceTransformer("jinaai/jina-embeddings-v3", trust_remote_code=True).to(device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
+
# Load and optimize model state dict
|
| 60 |
+
state_dict = torch.load(model_path, map_location=device)
|
|
|
|
| 61 |
|
| 62 |
# Dequantize if needed
|
| 63 |
for key, tensor in state_dict.items():
|
|
|
|
| 76 |
# Load FAISS index
|
| 77 |
def load_faiss_index():
|
| 78 |
print("Loading FAISS index...")
|
| 79 |
+
index = faiss.read_index(faiss_index_path)
|
|
|
|
|
|
|
| 80 |
print("FAISS index loaded successfully.")
|
| 81 |
return index
|
| 82 |
|
| 83 |
# Load dataset
|
| 84 |
def load_dataset():
|
| 85 |
print("Loading dataset...")
|
| 86 |
+
df = pd.read_csv(dataset_path)
|
|
|
|
|
|
|
| 87 |
print("Dataset loaded successfully.")
|
| 88 |
return df
|
| 89 |
|
|
|
|
| 94 |
self.model = model
|
| 95 |
self.pca = None
|
| 96 |
# Load PCA if it exists
|
| 97 |
+
pca_path = os.path.join(os.path.dirname(model_path), "pca_model.pkl")
|
| 98 |
if os.path.exists(pca_path):
|
| 99 |
with open(pca_path, 'rb') as f:
|
| 100 |
self.pca = pickle.load(f)
|
|
|
|
| 141 |
# Load tokenizer and LLM model
|
| 142 |
def load_tokenizer_and_model():
|
| 143 |
print("Loading tokenizer...")
|
| 144 |
+
tokenizer = AutoTokenizer.from_pretrained(model_dir)
|
| 145 |
print("Tokenizer loaded successfully.")
|
| 146 |
|
| 147 |
print("Loading LLM model...")
|
| 148 |
+
model_llm = AutoModelForCausalLM.from_pretrained(model_dir).to(device)
|
| 149 |
print("LLM model loaded successfully.")
|
| 150 |
return tokenizer, model_llm
|
| 151 |
|
|
|
|
| 363 |
print(f"Could not request results from Google Speech Recognition service; {e}")
|
| 364 |
return ""
|
| 365 |
|
| 366 |
+
# Flask app
|
| 367 |
+
app = Flask(__name__, template_folder="sample_data/templates")
|
| 368 |
+
conversation_context = {}
|
| 369 |
+
|
| 370 |
+
# Configure CORS
|
| 371 |
+
CORS(app, resources={
|
| 372 |
+
r"/*": {
|
| 373 |
+
"origins": ["http://localhost:4200", "https://localhost:4200"],
|
| 374 |
+
"methods": ["GET", "POST", "OPTIONS"],
|
| 375 |
+
"allow_headers": ["Content-Type", "X-Session-ID"]
|
| 376 |
+
}
|
| 377 |
+
})
|
| 378 |
+
|
| 379 |
+
@app.before_request
|
| 380 |
+
def handle_preflight():
|
| 381 |
+
if request.method == 'OPTIONS':
|
| 382 |
+
response = app.make_default_options_response()
|
| 383 |
+
response.headers.add('Access-Control-Allow-Headers', 'Content-Type, X-Session-ID')
|
| 384 |
+
response.headers.add('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
|
| 385 |
+
return response
|
| 386 |
+
|
| 387 |
@app.route('/')
|
| 388 |
def index():
|
| 389 |
+
print("Rendering index page")
|
| 390 |
return render_template('index.html')
|
| 391 |
|
| 392 |
@app.route('/search', methods=['POST'])
|
|
|
|
| 646 |
return jsonify({"error": f"Error processing location: {str(e)}"}), 500
|
| 647 |
|
| 648 |
if __name__ == '__main__':
|
| 649 |
+
# Remove ngrok configuration
|
| 650 |
+
# public_url = ngrok.connect(5000)
|
| 651 |
+
# print(f' * ngrok tunnel "http://127.0.0.1:5000" -> "{public_url}"')
|
| 652 |
+
|
| 653 |
+
# Update to use port 7860 (standard for Spaces)
|
| 654 |
app.run(host='0.0.0.0', port=7860)
|