Spaces:
Build error
Build error
Update rag_server.py
Browse files- rag_server.py +115 -201
rag_server.py
CHANGED
|
@@ -1,207 +1,121 @@
|
|
|
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
-
from
|
| 6 |
-
|
| 7 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 8 |
-
from langchain_core.documents import Document
|
| 9 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 10 |
from langchain_community.vectorstores import FAISS
|
| 11 |
-
|
| 12 |
-
from
|
| 13 |
-
from
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
return
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
def
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
"page": doc.metadata.get('page', 'N/A'),
|
| 80 |
-
"document_url": document_url,
|
| 81 |
-
"filename": os.path.basename(source_path) if source_path != 'N/A' else None
|
| 82 |
-
}
|
| 83 |
-
sources.append(source_info)
|
| 84 |
-
|
| 85 |
-
return {
|
| 86 |
-
"answer": result['result'].split("A:")[-1].strip() if "A:" in result['result'] else result['result'].strip(),
|
| 87 |
-
"sources": sources
|
| 88 |
-
}
|
| 89 |
-
|
| 90 |
-
@app.get("/v1/models")
|
| 91 |
-
def list_models():
|
| 92 |
-
return JSONResponse({
|
| 93 |
-
"object": "list",
|
| 94 |
-
"data": [
|
| 95 |
-
{
|
| 96 |
-
"id": "rag",
|
| 97 |
-
"object": "model",
|
| 98 |
-
"owned_by": "local",
|
| 99 |
-
}
|
| 100 |
-
]
|
| 101 |
-
})
|
| 102 |
-
|
| 103 |
-
@app.post("/v1/chat/completions")
|
| 104 |
-
async def openai_compatible_chat(request: Request):
|
| 105 |
-
payload = await request.json()
|
| 106 |
-
messages = payload.get("messages", [])
|
| 107 |
-
user_input = messages[-1]["content"] if messages else ""
|
| 108 |
-
stream = payload.get("stream", False)
|
| 109 |
-
|
| 110 |
-
result = ask_question(qa_chain, user_input)
|
| 111 |
-
answer = result['result']
|
| 112 |
-
|
| 113 |
-
# Process source document information
|
| 114 |
-
sources = []
|
| 115 |
-
for doc in result["source_documents"]:
|
| 116 |
-
source_path = doc.metadata.get('source', 'N/A')
|
| 117 |
-
document_url = get_document_url(source_path) if source_path != 'N/A' else None
|
| 118 |
-
filename = os.path.basename(source_path) if source_path != 'N/A' else None
|
| 119 |
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
final_answer = answer.split("A:")[-1].strip() if "A:" in answer else answer.strip()
|
| 139 |
-
final_answer += sources_md
|
| 140 |
-
|
| 141 |
-
if not stream:
|
| 142 |
-
return JSONResponse({
|
| 143 |
-
"id": f"chatcmpl-{uuid.uuid4()}",
|
| 144 |
-
"object": "chat.completion",
|
| 145 |
-
"choices": [{
|
| 146 |
-
"index": 0,
|
| 147 |
-
"message": {
|
| 148 |
-
"role": "assistant",
|
| 149 |
-
"content": final_answer
|
| 150 |
-
},
|
| 151 |
-
"finish_reason": "stop"
|
| 152 |
-
}],
|
| 153 |
-
"model": "rag",
|
| 154 |
-
})
|
| 155 |
-
|
| 156 |
-
# Generator for streaming response
|
| 157 |
-
def event_stream():
|
| 158 |
-
# Stream only the answer body first
|
| 159 |
-
answer_main = answer.split("A:")[-1].strip() if "A:" in answer else answer.strip()
|
| 160 |
-
for char in answer_main:
|
| 161 |
-
chunk = {
|
| 162 |
-
"id": f"chatcmpl-{uuid.uuid4()}",
|
| 163 |
-
"object": "chat.completion.chunk",
|
| 164 |
-
"choices": [{
|
| 165 |
-
"index": 0,
|
| 166 |
-
"delta": {
|
| 167 |
-
"content": char
|
| 168 |
-
},
|
| 169 |
-
"finish_reason": None
|
| 170 |
-
}]
|
| 171 |
-
}
|
| 172 |
-
yield f"data: {json.dumps(chunk)}\n\n"
|
| 173 |
-
time.sleep(0.005)
|
| 174 |
-
# Send reference documents (download links) all at once at the end
|
| 175 |
-
sources_md = "\nReferences Documents:\n"
|
| 176 |
-
seen = set()
|
| 177 |
-
for source in sources:
|
| 178 |
-
key = (source['filename'], source['document_url'])
|
| 179 |
-
if source['document_url'] and source['filename'] and key not in seen:
|
| 180 |
-
sources_md += f"Source: [{source['filename']}]({source['document_url']})\n"
|
| 181 |
-
seen.add(key)
|
| 182 |
-
if sources_md.strip() != "References Documents:":
|
| 183 |
-
chunk = {
|
| 184 |
-
"id": f"chatcmpl-{uuid.uuid4()}",
|
| 185 |
-
"object": "chat.completion.chunk",
|
| 186 |
-
"choices": [{
|
| 187 |
-
"index": 0,
|
| 188 |
-
"delta": {
|
| 189 |
-
"content": sources_md
|
| 190 |
-
},
|
| 191 |
-
"finish_reason": None
|
| 192 |
-
}]
|
| 193 |
-
}
|
| 194 |
-
yield f"data: {json.dumps(chunk)}\n\n"
|
| 195 |
-
done = {
|
| 196 |
-
"id": f"chatcmpl-{uuid.uuid4()}",
|
| 197 |
-
"object": "chat.completion.chunk",
|
| 198 |
-
"choices": [{
|
| 199 |
-
"index": 0,
|
| 200 |
-
"delta": {},
|
| 201 |
-
"finish_reason": "stop"
|
| 202 |
-
}]
|
| 203 |
-
}
|
| 204 |
-
yield f"data: {json.dumps(done)}\n\n"
|
| 205 |
-
return
|
| 206 |
-
|
| 207 |
-
return StreamingResponse(event_stream(), media_type="text/event-stream")
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
import os
|
| 3 |
+
import requests
|
| 4 |
+
from io import BytesIO
|
| 5 |
+
from PyPDF2 import PdfReader
|
| 6 |
+
from tempfile import NamedTemporaryFile
|
|
|
|
| 7 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
|
|
|
| 8 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 9 |
from langchain_community.vectorstores import FAISS
|
| 10 |
+
from huggingface_hub import InferenceClient
|
| 11 |
+
from gradio.exceptions import Error
|
| 12 |
+
from transformers import AutoModel
|
| 13 |
+
import streamlit as st
|
| 14 |
+
|
| 15 |
+
# --- Konfiguration ---
|
| 16 |
+
HF_API_TOKEN = os.environ.get("HF_API_TOKEN") # Lesen Sie den Token aus der Umgebungsvariable
|
| 17 |
+
MODEL_NAME = "dannyk97/mistral-screenplay-model"
|
| 18 |
+
|
| 19 |
+
# --- Hilfsfunktionen ---
|
| 20 |
+
|
| 21 |
+
def query_huggingface_inference_endpoints(prompt):
|
| 22 |
+
"""
|
| 23 |
+
Stellt eine Anfrage an die Hugging Face Inference API.
|
| 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"Fehler bei der Anfrage an Hugging Face API: {e}"
|
| 31 |
+
|
| 32 |
+
# Function to download PDF from Google Drive
|
| 33 |
+
def download_pdf_from_drive(drive_link):
|
| 34 |
+
file_id = drive_link.split('/d/')[1].split('/')[0]
|
| 35 |
+
download_url = f"https://drive.google.com/uc?id={file_id}&export=download"
|
| 36 |
+
response = requests.get(download_url)
|
| 37 |
+
if response.status_code == 200:
|
| 38 |
+
return BytesIO(response.content)
|
| 39 |
+
else:
|
| 40 |
+
raise Exception("Failed to download the PDF file from Google Drive.")
|
| 41 |
+
|
| 42 |
+
# Function to extract text from a PDF
|
| 43 |
+
def extract_text_from_pdf(pdf_stream):
|
| 44 |
+
pdf_reader = PdfReader(pdf_stream)
|
| 45 |
+
text = ""
|
| 46 |
+
for page in pdf_reader.pages:
|
| 47 |
+
text += page.extract_text()
|
| 48 |
+
return text
|
| 49 |
+
|
| 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 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 62 |
+
vector_db = FAISS.from_texts(chunks, embedding=embeddings)
|
| 63 |
+
return vector_db
|
| 64 |
+
|
| 65 |
+
# Function to query the vector database and interact with Hugging Face Inference API
|
| 66 |
+
def query_vector_db(query, vector_db):
|
| 67 |
+
# Retrieve relevant documents
|
| 68 |
+
docs = vector_db.similarity_search(query, k=3)
|
| 69 |
+
context = "\n".join([doc.page_content for doc in docs])
|
| 70 |
+
|
| 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) # Hier wurde das Modell nicht angegeben
|
| 75 |
+
return output
|
| 76 |
+
except Exception as e:
|
| 77 |
+
return f"FEHLER: {str(e)}"
|
| 78 |
+
|
| 79 |
+
# Streamlit app
|
| 80 |
+
st.title("RAG-Based Application with Google Drive Support")
|
| 81 |
+
|
| 82 |
+
# Predefined list of Google Drive links - HIER DEFINIERT!
|
| 83 |
+
drive_links = [
|
| 84 |
+
"https://drive.google.com/file/d/1PW8PJQC1EqYpsk8AhqrE4OS5cy57sqJ4/view?usp=drive_link"
|
| 85 |
+
# Add more links here as needed
|
| 86 |
+
]
|
| 87 |
+
|
| 88 |
+
st.write("Processing the predefined Google Drive links...")
|
| 89 |
+
|
| 90 |
+
all_chunks = []
|
| 91 |
+
|
| 92 |
+
# Process each predefined Google Drive link
|
| 93 |
+
for link in drive_links:
|
| 94 |
+
try:
|
| 95 |
+
st.write(f"Processing link: {link}")
|
| 96 |
+
# Download PDF
|
| 97 |
+
pdf_stream = download_pdf_from_drive(link)
|
| 98 |
+
st.write("PDF Downloaded Successfully!")
|
| 99 |
|
| 100 |
+
# Extract text
|
| 101 |
+
text = extract_text_from_pdf(pdf_stream)
|
| 102 |
+
st.write("PDF Text Extracted Successfully!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
+
# Chunk text
|
| 105 |
+
chunks = chunk_text(text)
|
| 106 |
+
st.write(f"Created {len(chunks)} text chunks.")
|
| 107 |
+
all_chunks.extend(chunks)
|
| 108 |
+
except Exception as e:
|
| 109 |
+
st.write(f"Error processing link {link}: {e}")
|
| 110 |
+
|
| 111 |
+
if all_chunks:
|
| 112 |
+
# Generate embeddings and store in FAISS
|
| 113 |
+
vector_db = create_embeddings_and_store(all_chunks)
|
| 114 |
+
st.write("Embeddings Generated and Stored Successfully!")
|
| 115 |
+
|
| 116 |
+
# User query input
|
| 117 |
+
user_query = st.text_input("Enter your query:")
|
| 118 |
+
if user_query:
|
| 119 |
+
response = query_vector_db(user_query, vector_db)
|
| 120 |
+
st.write("Response from LLM:")
|
| 121 |
+
st.write(response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|