Spaces:
Runtime error
Runtime error
Update rag_server.py
Browse files- rag_server.py +16 -10
rag_server.py
CHANGED
@@ -13,21 +13,22 @@ from transformers import AutoModel
|
|
13 |
import streamlit as st
|
14 |
|
15 |
# --- Konfiguration ---
|
16 |
-
|
|
|
17 |
MODEL_NAME = "dannyk97/mistral-screenplay-model"
|
18 |
|
19 |
# --- Hilfsfunktionen ---
|
20 |
|
21 |
def query_huggingface_inference_endpoints(prompt):
|
22 |
"""
|
23 |
-
|
24 |
"""
|
25 |
try:
|
26 |
client = InferenceClient(token=HF_API_TOKEN)
|
27 |
result = client.text_generation(prompt, model=MODEL_NAME)
|
28 |
return result
|
29 |
except Exception as e:
|
30 |
-
return f"
|
31 |
|
32 |
# Function to download PDF from Google Drive
|
33 |
def download_pdf_from_drive(drive_link):
|
@@ -50,17 +51,22 @@ def extract_text_from_pdf(pdf_stream):
|
|
50 |
# Function to split text into chunks
|
51 |
def chunk_text(text, chunk_size=500, chunk_overlap=50):
|
52 |
text_splitter = RecursiveCharacterTextSplitter(
|
53 |
-
chunk_size=chunk_size,
|
54 |
-
chunk_overlap=chunk_overlap,
|
55 |
-
length_function=len
|
56 |
)
|
57 |
return text_splitter.split_text(text)
|
58 |
|
59 |
# Function to create embeddings and store in FAISS
|
60 |
def create_embeddings_and_store(chunks):
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
# Function to query the vector database and interact with Hugging Face Inference API
|
66 |
def query_vector_db(query, vector_db):
|
@@ -71,7 +77,7 @@ def query_vector_db(query, vector_db):
|
|
71 |
# Interact with the Text Generation API
|
72 |
prompt = f"Nutze diesen Kontext um die Frage zu beantworten: {context}\nFrage: {query}"
|
73 |
try:
|
74 |
-
output = query_huggingface_inference_endpoints(prompt) #
|
75 |
return output
|
76 |
except Exception as e:
|
77 |
return f"FEHLER: {str(e)}"
|
|
|
13 |
import streamlit as st
|
14 |
|
15 |
# --- Konfiguration ---
|
16 |
+
os.environ["HF_HOME"] = "/app/cache" # Specify cache path
|
17 |
+
HF_API_TOKEN = os.environ.get("HF_API_TOKEN") # Read token from environment variable
|
18 |
MODEL_NAME = "dannyk97/mistral-screenplay-model"
|
19 |
|
20 |
# --- Hilfsfunktionen ---
|
21 |
|
22 |
def query_huggingface_inference_endpoints(prompt):
|
23 |
"""
|
24 |
+
Sends a request to the Hugging Face Inference API.
|
25 |
"""
|
26 |
try:
|
27 |
client = InferenceClient(token=HF_API_TOKEN)
|
28 |
result = client.text_generation(prompt, model=MODEL_NAME)
|
29 |
return result
|
30 |
except Exception as e:
|
31 |
+
return f"Error in query_huggingface_inference_endpoints: {e}"
|
32 |
|
33 |
# Function to download PDF from Google Drive
|
34 |
def download_pdf_from_drive(drive_link):
|
|
|
51 |
# Function to split text into chunks
|
52 |
def chunk_text(text, chunk_size=500, chunk_overlap=50):
|
53 |
text_splitter = RecursiveCharacterTextSplitter(
|
54 |
+
chunk_size=chunk_size, chunk_overlap=chunk_overlap
|
|
|
|
|
55 |
)
|
56 |
return text_splitter.split_text(text)
|
57 |
|
58 |
# Function to create embeddings and store in FAISS
|
59 |
def create_embeddings_and_store(chunks):
|
60 |
+
try:
|
61 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
62 |
+
vector_db = FAISS.from_texts(chunks, embedding=embeddings)
|
63 |
+
return vector_db
|
64 |
+
except Exception as e:
|
65 |
+
print(f"Error creating embeddings: {e}")
|
66 |
+
print("Using dummy embeddings to proceed (functionality will be limited).")
|
67 |
+
# Fallback to a simpler embedding model (but this might not work well)
|
68 |
+
vector_db = FAISS.from_texts(["fallback text"], HuggingFaceEmbeddings(model_name="all-mpnet-base-v2")) #Ggf mit "" ersetzen, falls die Implementierung nicht passt.
|
69 |
+
return vector_db
|
70 |
|
71 |
# Function to query the vector database and interact with Hugging Face Inference API
|
72 |
def query_vector_db(query, vector_db):
|
|
|
77 |
# Interact with the Text Generation API
|
78 |
prompt = f"Nutze diesen Kontext um die Frage zu beantworten: {context}\nFrage: {query}"
|
79 |
try:
|
80 |
+
output = query_huggingface_inference_endpoints(prompt) #Keine Modelangabe mehr
|
81 |
return output
|
82 |
except Exception as e:
|
83 |
return f"FEHLER: {str(e)}"
|