Spaces:
Sleeping
Sleeping
File size: 1,682 Bytes
d5ad9e6 b9a6c3b d5ad9e6 b9a6c3b d5ad9e6 b9a6c3b d5ad9e6 b9a6c3b d5ad9e6 b9a6c3b d5ad9e6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 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 |
# modules/qa_module.py
from transformers import pipeline
from typing import Dict, List
import torch
class EnhancedQAModule:
def __init__(
self,
model_name: str = "HuggingFaceH4/zephyr-7b-beta",
device: str = "cuda" if torch.cuda.is_available() else "cpu"
):
self.model = pipeline(
"question-answering",
model=model_name,
device=device,
model_kwargs={"torch_dtype": torch.float16 if device == "cuda" else torch.float32}
)
self.prompt_template = """
<|system|>
Answer the question based on the provided context. Be concise and specific.
If the answer cannot be found in the context, say so.
</s>
<|user|>
Context:
{context}
Question: {question}
</s>
<|assistant|>
"""
async def process(self, query: str, context_docs: List[Dict]) -> Dict:
# Combine context documents
context = "\n".join([f"[{doc['metadata']['source']}]: {doc['content']}"
for doc in context_docs])
# Format prompt
prompt = self.prompt_template.format(
context=context,
question=query
)
# Generate answer
response = self.model(
question=query,
context=context,
max_length=200,
num_beams=4,
temperature=0.7
)
return {
"answer": response["answer"],
"confidence": response["score"],
"sources": [doc["metadata"]["source"] for doc in context_docs]
} |