Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
2 |
+
from langchain.llms import HuggingFacePipeline
|
3 |
+
from langchain.prompts import PromptTemplate
|
4 |
+
from langchain.chains import RetrievalQA
|
5 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
6 |
+
from langchain_community.vectorstores import Chroma
|
7 |
+
from langchain_community.document_loaders import TextLoader
|
8 |
+
from langchain.text_splitter import CharacterTextSplitter
|
9 |
+
from langchain_community.document_loaders import WikipediaLoader
|
10 |
+
from transformers import pipeline
|
11 |
+
|
12 |
+
# Load T5-small model and tokenizer
|
13 |
+
model_name = "google-t5/t5-small"
|
14 |
+
tokenizer = T5Tokenizer.from_pretrained(model_name)
|
15 |
+
model = T5ForConditionalGeneration.from_pretrained(model_name)
|
16 |
+
|
17 |
+
# Create a text generation pipeline
|
18 |
+
text_generation_pipeline = pipeline(
|
19 |
+
"text2text-generation",
|
20 |
+
model=model,
|
21 |
+
tokenizer=tokenizer,
|
22 |
+
max_length=512,
|
23 |
+
temperature=0.7
|
24 |
+
)
|
25 |
+
|
26 |
+
# Create a LangChain LLM from the pipeline
|
27 |
+
llm = HuggingFacePipeline(pipeline=text_generation_pipeline)
|
28 |
+
|
29 |
+
# Load and process documents
|
30 |
+
#loader = TextLoader("https://en.wikipedia.org/wiki/Artificial_neuron")
|
31 |
+
|
32 |
+
|
33 |
+
# Load content from Wikipedia
|
34 |
+
loader = WikipediaLoader(query="Artificial neuron", load_max_docs=1)
|
35 |
+
documents = loader.load()
|
36 |
+
|
37 |
+
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
|
38 |
+
texts = text_splitter.split_documents(documents)
|
39 |
+
|
40 |
+
# Create embeddings and vector store
|
41 |
+
embeddings = HuggingFaceEmbeddings()
|
42 |
+
db = Chroma.from_documents(texts, embeddings)
|
43 |
+
|
44 |
+
# Create a retriever
|
45 |
+
retriever = db.as_retriever()
|
46 |
+
|
47 |
+
# Create a prompt template
|
48 |
+
template = """Use the following pieces of context to answer the question at the end.
|
49 |
+
If you don't know the answer, just say that you don't know, don't try to make up an answer.
|
50 |
+
Context: {context}
|
51 |
+
Question: {question}
|
52 |
+
Answer:"""
|
53 |
+
prompt = PromptTemplate(template=template, input_variables=["context", "question"])
|
54 |
+
|
55 |
+
# Create the RetrievalQA chain
|
56 |
+
qa_chain = RetrievalQA.from_chain_type(
|
57 |
+
llm=llm,
|
58 |
+
chain_type="stuff",
|
59 |
+
retriever=retriever,
|
60 |
+
return_source_documents=True,
|
61 |
+
chain_type_kwargs={"prompt": prompt}
|
62 |
+
)
|
63 |
+
|
64 |
+
# Example query
|
65 |
+
query = "What is an artificial neuron?"
|
66 |
+
result = qa_chain({"query": query})
|
67 |
+
|
68 |
+
print("Question:", query)
|
69 |
+
print("Answer:", result["result"])
|