File size: 3,096 Bytes
91c1e78
65881ce
 
 
 
 
 
 
 
91c1e78
65881ce
 
 
 
 
 
 
 
 
 
91c1e78
65881ce
 
91c1e78
65881ce
 
 
 
 
 
 
91c1e78
65881ce
 
 
 
 
91c1e78
65881ce
 
 
 
 
 
91c1e78
65881ce
 
 
 
 
 
91c1e78
65881ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91c1e78
65881ce
 
 
 
 
 
 
 
 
 
 
91c1e78
65881ce
 
 
 
 
 
 
 
91c1e78
 
65881ce
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import gradio as gr
import os
import PyPDF2
import pandas as pd
import openai
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores import FAISS
from langchain.llms import OpenAI

def detect_language(text):
    """Detects the language of the input text using OpenAI."""
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=[
            {"role": "system", "content": "Detect the language of this text."},
            {"role": "user", "content": text}
        ]
    )
    return response["choices"][0]["message"]["content"].strip()

# Set up OpenAI API key (replace with your key)
openai.api_key = "YOUR_OPENAI_API_KEY"

def get_text_from_pdf(pdf_files):
    text = ""
    for pdf in pdf_files:
        reader = PyPDF2.PdfReader(pdf)
        for page in reader.pages:
            text += page.extract_text() + "\n"
    return text

def get_text_from_txt(txt_files):
    text = ""
    for txt in txt_files:
        text += txt.read().decode("utf-8") + "\n"
    return text

def get_text_from_csv(csv_files):
    text = ""
    for csv in csv_files:
        df = pd.read_csv(csv)
        text += df.to_string() + "\n"
    return text

def create_vector_database(text):
    splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
    texts = splitter.split_text(text)
    embeddings = OpenAIEmbeddings()
    vector_db = FAISS.from_texts(texts, embeddings)
    return vector_db

def get_answer(question, vector_db):
    retriever = vector_db.as_retriever()
    docs = retriever.get_relevant_documents(question)
    
    if not docs:
        return "I could not find the answer in the documents. Do you want me to search external sources?"
    
    context = "\n".join([doc.page_content for doc in docs])
    language = detect_language(question)
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=[
            {"role": "system", "content": f"You are a Data Analytics assistant. Answer in {language}. Use the documents to answer questions."},
            {"role": "user", "content": question + "\n\nBased on the following context:\n" + context}
        ]
    )
    return response["choices"][0]["message"]["content"]

def chatbot_interface(pdf_files, txt_files, csv_files, question):
    text = ""
    text += get_text_from_pdf(pdf_files) if pdf_files else ""
    text += get_text_from_txt(txt_files) if txt_files else ""
    text += get_text_from_csv(csv_files) if csv_files else ""
    
    if not text:
        return "Please upload files before asking questions."
    
    vector_db = create_vector_database(text)
    return get_answer(question, vector_db)

# Gradio interface
demo = gr.Interface(
    fn=chatbot_interface,
    inputs=[gr.File(file_types=[".pdf"], multiple=True),
            gr.File(file_types=[".txt"], multiple=True),
            gr.File(file_types=[".csv"], multiple=True),
            gr.Textbox(placeholder="Type your question here...")],
    outputs=gr.Textbox()
)

demo.launch()