File size: 2,127 Bytes
e878e8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
61
62
63
64
65
66
67
"""
This works.

reference: https://www.youtube.com/watch?v=CPgp8MhmGVY
"""

# Imports
import gradio as gr
from langchain_community.llms import Ollama
from langchain_community.document_loaders import WebBaseLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter

# dont use this embeddings
# from langchain.embeddings import GPT4AllEmbeddings
# from langchain_community.embeddings import GPT4AllEmbeddings

# from langchain.embeddings import SentenceTransformerEmbeddings
from langchain_community.embeddings import SentenceTransformerEmbeddings

# from langchain.vectorstores import Chroma
from langchain_community.vectorstores import Chroma

from langchain.chains import RetrievalQA


# Instantiate the Ollama model
ollama = Ollama(
    # base_url="http://localhost:3000",
    model="llama3"
)

# Load the webpage content using WebBaseLoader
loader = WebBaseLoader("https://en.wikipedia.org/wiki/Tsai_Ing-wen")
data = loader.load()

# Split the text into manageable chunks using RecursiveCharacterTextSplitter
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=0)
all_splits = text_splitter.split_documents(data)

# Instantiate SentenceTransformers embeddings with the specified model
sentence_embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")

# Create the vector store from the document splits and embeddings
vectorstore = Chroma.from_documents(documents=all_splits, embedding=sentence_embeddings)

# Create a QA chain using the Ollama model and the vector store retriever
qachain = RetrievalQA.from_chain_type(ollama, retriever=vectorstore.as_retriever())


# Function to handle the question and return the answer
def answer_question(question):
    response = qachain({"query": question})
    return response["result"]


# Create a Gradio interface
interface = gr.Interface(
    fn=answer_question,
    inputs="text",
    outputs="text",
    title="RAG Model QA Interface",
    description="Ask a question about Tsai Ing-wen and get an answer from the Retrieval-Augmented Generation model.",
)

# Launch the Gradio interface
interface.launch(share=True)