Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
CHANGED
@@ -6,14 +6,15 @@ import gradio as gr
|
|
6 |
from sentence_transformers import SentenceTransformer
|
7 |
from langchain_community.vectorstores import FAISS
|
8 |
from langchain.embeddings.base import Embeddings
|
|
|
9 |
from tqdm import tqdm
|
10 |
|
11 |
# Configuration
|
12 |
-
QWEN_API_URL = os.getenv("QWEN_API_URL", "Qwen/Qwen2.5-Max-Demo") #
|
13 |
CHUNK_SIZE = 800
|
14 |
TOP_K_RESULTS = 150
|
15 |
SIMILARITY_THRESHOLD = 0.4
|
16 |
-
PASSWORD_HASH = os.getenv("PASSWORD_HASH", "abc12345") #
|
17 |
|
18 |
BASE_SYSTEM_PROMPT = """
|
19 |
Répondez en français selon ces règles :
|
@@ -104,13 +105,9 @@ def create_new_database(file_content: str, db_name: str, password: str, progress
|
|
104 |
return "No valid chunks generated. Database creation failed.", []
|
105 |
|
106 |
logging.info(f"Creating {len(chunks)} chunks...")
|
107 |
-
progress(0, desc="Starting embedding process...")
|
108 |
|
109 |
# Create embeddings with progress tracking
|
110 |
-
embeddings_list = []
|
111 |
-
for i, chunk in enumerate(chunks):
|
112 |
-
progress(i / len(chunks), desc=f"Embedding chunk {i+1}/{len(chunks)}")
|
113 |
-
embeddings_list.append(embeddings.embed_query(chunk))
|
114 |
|
115 |
# Create FAISS database
|
116 |
vector_store = FAISS.from_embeddings(
|
@@ -121,13 +118,6 @@ def create_new_database(file_content: str, db_name: str, password: str, progress
|
|
121 |
# Save FAISS database locally
|
122 |
vector_store.save_local(".")
|
123 |
|
124 |
-
# Verify files were created successfully
|
125 |
-
if not os.path.exists(faiss_file) or not os.path.exists(pkl_file):
|
126 |
-
return "Failed to save FAISS database files. Please check file permissions.", []
|
127 |
-
|
128 |
-
logging.info(f"FAISS database files created: {faiss_file}, {pkl_file}")
|
129 |
-
|
130 |
-
# Update the list of available databases
|
131 |
db_list = [os.path.splitext(f)[0].replace("-index", "") for f in os.listdir(".") if f.endswith(".faiss")]
|
132 |
|
133 |
return f"Database '{db_name}' created successfully.", db_list
|
@@ -137,7 +127,7 @@ def create_new_database(file_content: str, db_name: str, password: str, progress
|
|
137 |
return f"Error creating database: {str(e)}", []
|
138 |
|
139 |
def generate_response(user_input: str, db_name: str) -> str:
|
140 |
-
"""Generate response using Qwen2.5
|
141 |
try:
|
142 |
if not db_name:
|
143 |
return "Please select a database to chat with."
|
@@ -148,18 +138,11 @@ def generate_response(user_input: str, db_name: str) -> str:
|
|
148 |
if not os.path.exists(faiss_file) or not os.path.exists(pkl_file):
|
149 |
return f"Database '{db_name}' does not exist."
|
150 |
|
151 |
-
vector_store = FAISS.load_local(".", embeddings
|
152 |
|
153 |
-
|
154 |
-
docs_scores = vector_store.similarity_search_with_score(
|
155 |
-
user_input,
|
156 |
-
k=TOP_K_RESULTS * 3
|
157 |
-
)
|
158 |
|
159 |
-
filtered_docs = [
|
160 |
-
(doc, score) for doc, score in docs_scores
|
161 |
-
if score < SIMILARITY_THRESHOLD
|
162 |
-
]
|
163 |
|
164 |
filtered_docs.sort(key=lambda x: x[1])
|
165 |
|
@@ -168,12 +151,9 @@ def generate_response(user_input: str, db_name: str) -> str:
|
|
168 |
|
169 |
best_docs = [doc for doc, _ in filtered_docs[:TOP_K_RESULTS]]
|
170 |
|
171 |
-
context = "\n".join(
|
172 |
-
f"=== Source {i+1} ===\n{doc.page_content}\n"
|
173 |
-
for i, doc in enumerate(best_docs)
|
174 |
-
)
|
175 |
|
176 |
-
client = Client(QWEN_API_URL
|
177 |
|
178 |
response = client.predict(
|
179 |
query=user_input,
|
@@ -190,27 +170,20 @@ def generate_response(user_input: str, db_name: str) -> str:
|
|
190 |
return "Réponse indisponible - Veuillez reformuler votre question."
|
191 |
|
192 |
except Exception as e:
|
193 |
-
logging.error(f"
|
194 |
-
return "Erreur de génération
|
195 |
|
196 |
-
# Initialize models and
|
197 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
198 |
-
model = SentenceTransformer("cnmoro/snowflake-arctic-embed-m-v2.0-cpu", device=device
|
199 |
embeddings = LocalEmbeddings(model)
|
200 |
|
201 |
-
# Gradio interface setup remains unchanged from your original code.
|
202 |
with gr.Blocks() as app:
|
203 |
-
gr.Markdown("#
|
204 |
-
|
205 |
-
# Shared state for database list
|
206 |
-
db_list_state = gr.State([])
|
207 |
-
|
208 |
-
def update_db_list():
|
209 |
-
"""Update the list of available databases"""
|
210 |
-
return [os.path.splitext(f)[0].replace("-index", "") for f in os.listdir(".") if f.endswith(".faiss")]
|
211 |
-
|
212 |
with gr.Tab("Create Database"):
|
213 |
-
|
214 |
-
|
215 |
if __name__ == "__main__":
|
216 |
app.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
6 |
from sentence_transformers import SentenceTransformer
|
7 |
from langchain_community.vectorstores import FAISS
|
8 |
from langchain.embeddings.base import Embeddings
|
9 |
+
from gradio_client import Client
|
10 |
from tqdm import tqdm
|
11 |
|
12 |
# Configuration
|
13 |
+
QWEN_API_URL = os.getenv("QWEN_API_URL", "https://huggingface.co/spaces/Qwen/Qwen2.5-Max-Demo") # Ensure this URL points to the correct Gradio Space API endpoint.
|
14 |
CHUNK_SIZE = 800
|
15 |
TOP_K_RESULTS = 150
|
16 |
SIMILARITY_THRESHOLD = 0.4
|
17 |
+
PASSWORD_HASH = os.getenv("PASSWORD_HASH", "abc12345") # Use an environment variable for security
|
18 |
|
19 |
BASE_SYSTEM_PROMPT = """
|
20 |
Répondez en français selon ces règles :
|
|
|
105 |
return "No valid chunks generated. Database creation failed.", []
|
106 |
|
107 |
logging.info(f"Creating {len(chunks)} chunks...")
|
|
|
108 |
|
109 |
# Create embeddings with progress tracking
|
110 |
+
embeddings_list = [embeddings.embed_query(chunk) for chunk in tqdm(chunks)]
|
|
|
|
|
|
|
111 |
|
112 |
# Create FAISS database
|
113 |
vector_store = FAISS.from_embeddings(
|
|
|
118 |
# Save FAISS database locally
|
119 |
vector_store.save_local(".")
|
120 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
db_list = [os.path.splitext(f)[0].replace("-index", "") for f in os.listdir(".") if f.endswith(".faiss")]
|
122 |
|
123 |
return f"Database '{db_name}' created successfully.", db_list
|
|
|
127 |
return f"Error creating database: {str(e)}", []
|
128 |
|
129 |
def generate_response(user_input: str, db_name: str) -> str:
|
130 |
+
"""Generate response using Qwen2.5-Max Demo API"""
|
131 |
try:
|
132 |
if not db_name:
|
133 |
return "Please select a database to chat with."
|
|
|
138 |
if not os.path.exists(faiss_file) or not os.path.exists(pkl_file):
|
139 |
return f"Database '{db_name}' does not exist."
|
140 |
|
141 |
+
vector_store = FAISS.load_local(".", embeddings)
|
142 |
|
143 |
+
docs_scores = vector_store.similarity_search_with_score(user_input, k=TOP_K_RESULTS * 3)
|
|
|
|
|
|
|
|
|
144 |
|
145 |
+
filtered_docs = [(doc, score) for doc, score in docs_scores if score < SIMILARITY_THRESHOLD]
|
|
|
|
|
|
|
146 |
|
147 |
filtered_docs.sort(key=lambda x: x[1])
|
148 |
|
|
|
151 |
|
152 |
best_docs = [doc for doc, _ in filtered_docs[:TOP_K_RESULTS]]
|
153 |
|
154 |
+
context = "\n".join(f"=== Source {i+1} ===\n{doc.page_content}\n" for i, doc in enumerate(best_docs))
|
|
|
|
|
|
|
155 |
|
156 |
+
client = Client(QWEN_API_URL)
|
157 |
|
158 |
response = client.predict(
|
159 |
query=user_input,
|
|
|
170 |
return "Réponse indisponible - Veuillez reformuler votre question."
|
171 |
|
172 |
except Exception as e:
|
173 |
+
logging.error(f"Error generating response: {str(e)}")
|
174 |
+
return "Erreur de génération."
|
175 |
|
176 |
+
# Initialize models and Gradio app
|
177 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
178 |
+
model = SentenceTransformer("cnmoro/snowflake-arctic-embed-m-v2.0-cpu", device=device)
|
179 |
embeddings = LocalEmbeddings(model)
|
180 |
|
|
|
181 |
with gr.Blocks() as app:
|
182 |
+
gr.Markdown("# Knowledge Assistant")
|
183 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
184 |
with gr.Tab("Create Database"):
|
185 |
+
# Database creation UI setup
|
186 |
+
|
187 |
if __name__ == "__main__":
|
188 |
app.launch(server_name="0.0.0.0", server_port=7860)
|
189 |
+
|