Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,8 @@ import os
|
|
3 |
import PyPDF2
|
4 |
import pandas as pd
|
5 |
import openai
|
|
|
|
|
6 |
from langchain_community.embeddings import OpenAIEmbeddings
|
7 |
from langchain_community.vectorstores import FAISS
|
8 |
from langchain_community.llms import OpenAI
|
@@ -21,6 +23,20 @@ def detect_language(text):
|
|
21 |
# Set up OpenAI API key (replace with your key)
|
22 |
openai.api_key = "YOUR_OPENAI_API_KEY"
|
23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
def get_text_from_pdf(pdf_files):
|
25 |
text = ""
|
26 |
for pdf in pdf_files:
|
@@ -67,14 +83,16 @@ def get_answer(question, vector_db):
|
|
67 |
)
|
68 |
return response["choices"][0]["message"]["content"]
|
69 |
|
70 |
-
def chatbot_interface(
|
71 |
text = ""
|
72 |
-
|
73 |
-
|
74 |
-
|
|
|
|
|
75 |
|
76 |
if not text:
|
77 |
-
return "Please upload
|
78 |
|
79 |
vector_db = create_vector_database(text)
|
80 |
return get_answer(question, vector_db)
|
@@ -82,9 +100,7 @@ def chatbot_interface(pdf_files, txt_files, csv_files, question):
|
|
82 |
# Gradio interface
|
83 |
demo = gr.Interface(
|
84 |
fn=chatbot_interface,
|
85 |
-
inputs=[gr.File(file_types=[".
|
86 |
-
gr.File(file_types=[".txt"]),
|
87 |
-
gr.File(file_types=[".csv"]),
|
88 |
gr.Textbox(placeholder="Type your question here...")],
|
89 |
outputs=gr.Textbox()
|
90 |
)
|
|
|
3 |
import PyPDF2
|
4 |
import pandas as pd
|
5 |
import openai
|
6 |
+
import zipfile
|
7 |
+
from io import BytesIO
|
8 |
from langchain_community.embeddings import OpenAIEmbeddings
|
9 |
from langchain_community.vectorstores import FAISS
|
10 |
from langchain_community.llms import OpenAI
|
|
|
23 |
# Set up OpenAI API key (replace with your key)
|
24 |
openai.api_key = "YOUR_OPENAI_API_KEY"
|
25 |
|
26 |
+
def extract_files_from_zip(zip_file):
|
27 |
+
"""Extracts PDF, TXT, and CSV files from a ZIP archive."""
|
28 |
+
extracted_files = {"pdf": [], "txt": [], "csv": []}
|
29 |
+
with zipfile.ZipFile(zip_file, 'r') as zip_ref:
|
30 |
+
for file_name in zip_ref.namelist():
|
31 |
+
with zip_ref.open(file_name) as file:
|
32 |
+
if file_name.endswith(".pdf"):
|
33 |
+
extracted_files["pdf"].append(BytesIO(file.read()))
|
34 |
+
elif file_name.endswith(".txt"):
|
35 |
+
extracted_files["txt"].append(BytesIO(file.read()))
|
36 |
+
elif file_name.endswith(".csv"):
|
37 |
+
extracted_files["csv"].append(BytesIO(file.read()))
|
38 |
+
return extracted_files
|
39 |
+
|
40 |
def get_text_from_pdf(pdf_files):
|
41 |
text = ""
|
42 |
for pdf in pdf_files:
|
|
|
83 |
)
|
84 |
return response["choices"][0]["message"]["content"]
|
85 |
|
86 |
+
def chatbot_interface(zip_file, question):
|
87 |
text = ""
|
88 |
+
if zip_file:
|
89 |
+
extracted_files = extract_files_from_zip(zip_file)
|
90 |
+
text += get_text_from_pdf(extracted_files["pdf"])
|
91 |
+
text += get_text_from_txt(extracted_files["txt"])
|
92 |
+
text += get_text_from_csv(extracted_files["csv"])
|
93 |
|
94 |
if not text:
|
95 |
+
return "Please upload a ZIP file containing PDFs, TXTs, or CSVs before asking questions."
|
96 |
|
97 |
vector_db = create_vector_database(text)
|
98 |
return get_answer(question, vector_db)
|
|
|
100 |
# Gradio interface
|
101 |
demo = gr.Interface(
|
102 |
fn=chatbot_interface,
|
103 |
+
inputs=[gr.File(file_types=[".zip"]),
|
|
|
|
|
104 |
gr.Textbox(placeholder="Type your question here...")],
|
105 |
outputs=gr.Textbox()
|
106 |
)
|