File size: 1,638 Bytes
32952b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import openai
from pathlib import Path

from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.indexes import VectorstoreIndexCreator
from langchain.document_loaders import TextLoader
from langchain.chat_models import ChatOpenAI

import gradio as gr

def index_txt(directory):
    files = directory.glob("*.txt")
    loaders = [TextLoader(str(file)) for file in files]
    return VectorstoreIndexCreator().from_loaders(loaders)

def vector_search(natural_lang_query):
    llm = ChatOpenAI(temperature=0, model_name="gpt-4")
    query_result = index.query_with_sources(natural_lang_query, llm=llm)
    final_result = "Answer: " + query_result['answer']
    final_result += f"\n Sources: {query_result['sources']}"
    return final_result

def create_gradio_interface(title, description):
    """Create a Gradio interface with a single text input and a single text output."""
    interface = gr.Interface(
        fn=vector_search,
        inputs=[
            gr.inputs.Textbox(label="What would you like to ask your data?")
        ],
        outputs=gr.outputs.Textbox(label="Results"),
        title=title,
        description=description
    )
    return interface

# Define path for documents
output_dir = Path("docs/")

# Launch the interface
index = index_txt(output_dir)

interface = create_gradio_interface(title="ChatBot - Question answering across Pfizer Comirnaty Documents",
    description=(
        "Semantic search: Enter a query to receive an answer with cited source documents.\n\n"
        "DISCLAIMER: This is an early alpha product and not intended for production use.")
)
interface.launch()