Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,6 @@ from langchain.text_splitter import CharacterTextSplitter
|
|
| 6 |
from langchain_community.vectorstores import FAISS
|
| 7 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 8 |
from langchain_core.prompts import PromptTemplate
|
| 9 |
-
from langchain_community.llms import HuggingFacePipeline
|
| 10 |
|
| 11 |
# Hugging Face Transformers
|
| 12 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
|
@@ -15,7 +14,7 @@ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
|
| 15 |
# ---------------- Load LLM with fallback ----------------
|
| 16 |
def load_llm():
|
| 17 |
model_ids = [
|
| 18 |
-
"google/flan-t5-base",
|
| 19 |
"google/flan-t5-small",
|
| 20 |
"google/flan-t5-large"
|
| 21 |
]
|
|
@@ -26,7 +25,7 @@ def load_llm():
|
|
| 26 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 27 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_id)
|
| 28 |
|
| 29 |
-
# Create pipeline without
|
| 30 |
pipe = pipeline(
|
| 31 |
"text2text-generation",
|
| 32 |
model=model,
|
|
@@ -34,17 +33,15 @@ def load_llm():
|
|
| 34 |
max_length=512
|
| 35 |
)
|
| 36 |
print(f"✅ Successfully loaded model: {model_id}")
|
| 37 |
-
|
| 38 |
-
# Create HuggingFacePipeline without any extra parameters
|
| 39 |
-
return HuggingFacePipeline(pipeline=pipe)
|
| 40 |
except Exception as e:
|
| 41 |
print(f"⚠️ Failed to load {model_id}: {e}")
|
| 42 |
continue
|
| 43 |
|
| 44 |
-
raise RuntimeError("❌ No model could be loaded.
|
| 45 |
|
| 46 |
|
| 47 |
-
|
| 48 |
|
| 49 |
|
| 50 |
# ---------------- Process PDF ----------------
|
|
@@ -88,13 +85,17 @@ def ask_question(pdf_files, question):
|
|
| 88 |
context = "\n".join([doc.page_content for doc in docs])
|
| 89 |
|
| 90 |
# Prompt template
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
)
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
return response if response else "⚠️ No answer generated. Try another question."
|
| 100 |
|
|
@@ -126,13 +127,4 @@ with gr.Blocks() as demo:
|
|
| 126 |
submit_btn = gr.Button("Ask Question", variant="primary")
|
| 127 |
submit_btn.click(fn=ask_question, inputs=[pdf_input, question_input], outputs=output)
|
| 128 |
|
| 129 |
-
# Add examples
|
| 130 |
-
gr.Examples(
|
| 131 |
-
examples=[
|
| 132 |
-
["What is the main topic of this document?", "Summarize the key points"],
|
| 133 |
-
["What are the main findings?", "Who are the authors?"]
|
| 134 |
-
],
|
| 135 |
-
inputs=question_input
|
| 136 |
-
)
|
| 137 |
-
|
| 138 |
demo.launch()
|
|
|
|
| 6 |
from langchain_community.vectorstores import FAISS
|
| 7 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 8 |
from langchain_core.prompts import PromptTemplate
|
|
|
|
| 9 |
|
| 10 |
# Hugging Face Transformers
|
| 11 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
|
|
|
| 14 |
# ---------------- Load LLM with fallback ----------------
|
| 15 |
def load_llm():
|
| 16 |
model_ids = [
|
| 17 |
+
"google/flan-t5-base",
|
| 18 |
"google/flan-t5-small",
|
| 19 |
"google/flan-t5-large"
|
| 20 |
]
|
|
|
|
| 25 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 26 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_id)
|
| 27 |
|
| 28 |
+
# Create pipeline directly without LangChain wrapper
|
| 29 |
pipe = pipeline(
|
| 30 |
"text2text-generation",
|
| 31 |
model=model,
|
|
|
|
| 33 |
max_length=512
|
| 34 |
)
|
| 35 |
print(f"✅ Successfully loaded model: {model_id}")
|
| 36 |
+
return pipe
|
|
|
|
|
|
|
| 37 |
except Exception as e:
|
| 38 |
print(f"⚠️ Failed to load {model_id}: {e}")
|
| 39 |
continue
|
| 40 |
|
| 41 |
+
raise RuntimeError("❌ No model could be loaded.")
|
| 42 |
|
| 43 |
|
| 44 |
+
llm_pipeline = load_llm()
|
| 45 |
|
| 46 |
|
| 47 |
# ---------------- Process PDF ----------------
|
|
|
|
| 85 |
context = "\n".join([doc.page_content for doc in docs])
|
| 86 |
|
| 87 |
# Prompt template
|
| 88 |
+
prompt_template = f"""Answer the question based on the following context:
|
| 89 |
+
|
| 90 |
+
Context: {context}
|
|
|
|
| 91 |
|
| 92 |
+
Question: {question}
|
| 93 |
+
|
| 94 |
+
Answer:"""
|
| 95 |
+
|
| 96 |
+
# Use the pipeline directly
|
| 97 |
+
result = llm_pipeline(prompt_template, max_length=512, do_sample=False)
|
| 98 |
+
response = result[0]['generated_text'].strip()
|
| 99 |
|
| 100 |
return response if response else "⚠️ No answer generated. Try another question."
|
| 101 |
|
|
|
|
| 127 |
submit_btn = gr.Button("Ask Question", variant="primary")
|
| 128 |
submit_btn.click(fn=ask_question, inputs=[pdf_input, question_input], outputs=output)
|
| 129 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
demo.launch()
|