Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
CHANGED
@@ -18,7 +18,7 @@ QWEN_API_URL = "Qwen/Qwen2.5-Max-Demo" # Gradio API for Qwen2.5 chat
|
|
18 |
CHUNK_SIZE = 800
|
19 |
TOP_K_RESULTS = 150
|
20 |
SIMILARITY_THRESHOLD = 0.4
|
21 |
-
|
22 |
|
23 |
BASE_SYSTEM_PROMPT = """
|
24 |
Répondez en français selon ces règles :
|
@@ -76,7 +76,7 @@ def split_text_into_chunks(text: str) -> List[str]:
|
|
76 |
chunk.rfind('\n\n')
|
77 |
)
|
78 |
|
79 |
-
if last_punct != -1 and (end - start) > CHUNK_SIZE//2:
|
80 |
end = start + last_punct + 1
|
81 |
|
82 |
chunks.append(text[start:end].strip())
|
@@ -105,7 +105,7 @@ def initialize_vector_store(embeddings: Embeddings, db_name: str) -> FAISS:
|
|
105 |
|
106 |
def create_new_database(file_content: str, db_name: str, password: str) -> str:
|
107 |
"""Create a new FAISS database from uploaded file"""
|
108 |
-
if password !=
|
109 |
return "Incorrect password. Database creation failed."
|
110 |
|
111 |
if not file_content.strip():
|
@@ -149,7 +149,7 @@ def generate_response(user_input: str, db_name: str) -> Optional[str]:
|
|
149 |
# Contextual search
|
150 |
docs_scores = vector_store.similarity_search_with_score(
|
151 |
user_input,
|
152 |
-
k=TOP_K_RESULTS*3
|
153 |
)
|
154 |
|
155 |
# Filter results
|
@@ -191,7 +191,7 @@ def generate_response(user_input: str, db_name: str) -> Optional[str]:
|
|
191 |
logging.error(f"Generation error: {str(e)}", exc_info=True)
|
192 |
return None
|
193 |
|
194 |
-
# Initialize models
|
195 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
196 |
model = SentenceTransformer("cnmoro/snowflake-arctic-embed-m-v2.0-cpu", device=device, trust_remote_code=True)
|
197 |
embeddings = LocalEmbeddings(model)
|
@@ -199,7 +199,7 @@ embeddings = LocalEmbeddings(model)
|
|
199 |
# Gradio interface
|
200 |
with gr.Blocks() as app:
|
201 |
gr.Markdown("# Local Tech Knowledge Assistant")
|
202 |
-
|
203 |
with gr.Tab("Create Database"):
|
204 |
gr.Markdown("## Create a New FAISS Database")
|
205 |
file_input = gr.File(label="Upload .txt File")
|
@@ -213,7 +213,11 @@ with gr.Blocks() as app:
|
|
213 |
return "Please provide all required inputs."
|
214 |
|
215 |
# Read file content
|
216 |
-
|
|
|
|
|
|
|
|
|
217 |
return create_new_database(file_content, db_name, password)
|
218 |
|
219 |
create_button.click(
|
@@ -221,7 +225,7 @@ with gr.Blocks() as app:
|
|
221 |
inputs=[file_input, db_name_input, password_input],
|
222 |
outputs=create_output
|
223 |
)
|
224 |
-
|
225 |
with gr.Tab("Chat with Database"):
|
226 |
gr.Markdown("## Chat with Existing Databases")
|
227 |
db_select = gr.Dropdown(choices=[], label="Select Database")
|
|
|
18 |
CHUNK_SIZE = 800
|
19 |
TOP_K_RESULTS = 150
|
20 |
SIMILARITY_THRESHOLD = 0.4
|
21 |
+
PASSWORD_HASH = "abc12345" # Replace with hashed password in production
|
22 |
|
23 |
BASE_SYSTEM_PROMPT = """
|
24 |
Répondez en français selon ces règles :
|
|
|
76 |
chunk.rfind('\n\n')
|
77 |
)
|
78 |
|
79 |
+
if last_punct != -1 and (end - start) > CHUNK_SIZE // 2:
|
80 |
end = start + last_punct + 1
|
81 |
|
82 |
chunks.append(text[start:end].strip())
|
|
|
105 |
|
106 |
def create_new_database(file_content: str, db_name: str, password: str) -> str:
|
107 |
"""Create a new FAISS database from uploaded file"""
|
108 |
+
if password != PASSWORD_HASH:
|
109 |
return "Incorrect password. Database creation failed."
|
110 |
|
111 |
if not file_content.strip():
|
|
|
149 |
# Contextual search
|
150 |
docs_scores = vector_store.similarity_search_with_score(
|
151 |
user_input,
|
152 |
+
k=TOP_K_RESULTS * 3
|
153 |
)
|
154 |
|
155 |
# Filter results
|
|
|
191 |
logging.error(f"Generation error: {str(e)}", exc_info=True)
|
192 |
return None
|
193 |
|
194 |
+
# Initialize models and vector store
|
195 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
196 |
model = SentenceTransformer("cnmoro/snowflake-arctic-embed-m-v2.0-cpu", device=device, trust_remote_code=True)
|
197 |
embeddings = LocalEmbeddings(model)
|
|
|
199 |
# Gradio interface
|
200 |
with gr.Blocks() as app:
|
201 |
gr.Markdown("# Local Tech Knowledge Assistant")
|
202 |
+
|
203 |
with gr.Tab("Create Database"):
|
204 |
gr.Markdown("## Create a New FAISS Database")
|
205 |
file_input = gr.File(label="Upload .txt File")
|
|
|
213 |
return "Please provide all required inputs."
|
214 |
|
215 |
# Read file content
|
216 |
+
try:
|
217 |
+
file_content = file.read().decode("utf-8")
|
218 |
+
except Exception as e:
|
219 |
+
return f"Error reading file: {str(e)}"
|
220 |
+
|
221 |
return create_new_database(file_content, db_name, password)
|
222 |
|
223 |
create_button.click(
|
|
|
225 |
inputs=[file_input, db_name_input, password_input],
|
226 |
outputs=create_output
|
227 |
)
|
228 |
+
|
229 |
with gr.Tab("Chat with Database"):
|
230 |
gr.Markdown("## Chat with Existing Databases")
|
231 |
db_select = gr.Dropdown(choices=[], label="Select Database")
|