File size: 1,500 Bytes
6929c27
09ae247
6929c27
09ae247
 
 
 
6929c27
09ae247
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from src.rag import RAG

import bs4
from langchain_community.document_loaders import WebBaseLoader
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from langchain_text_splitters import RecursiveCharacterTextSplitter

from dotenv import load_dotenv

# set the required env variables
load_dotenv(".env")

def rag_handler(web_paths, model_name, temperature, question):
    print(web_paths)
    web_paths = web_paths.split(',')
    print(web_paths)
    loader = WebBaseLoader(
        web_paths=web_paths,
        bs_kwargs=dict(
            parse_only=bs4.SoupStrainer(
                class_=("post-content", "post-title", "post-header")
            )
        ),
    )

    llm = ChatOpenAI(model_name=model_name, temperature=temperature)
    text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200) # TODO: Parameterize this
    
    rag_pipeline = RAG(llm, loader, text_splitter, OpenAIEmbeddings)

    return rag_pipeline.invoke(question)

def create_rag_interface():
    return gr.Interface(
        fn=rag_handler,
        inputs=[
            gr.Textbox(value="https://lilianweng.github.io/posts/2023-06-23-agent/"),
            gr.Dropdown(["gpt-3.5-turbo"], type="value"),
            gr.Slider(0, 1, step=0.1),
            'text'
        ],
        outputs="text"
    )


if __name__ == '__main__':
    interface_list = []
    interface_list.append(create_rag_interface())

    demo = gr.TabbedInterface(interface_list)

    demo.launch()