-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
218 lines (164 loc) · 7.02 KB
/
main.py
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import os
import gradio as gr
import PyPDF2
import docx
# from embeddings import load_and_split_document,query_rag
from embeddings import query_bq
import threading
multipleFilecontent = ''
def extract_txt(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
return file.read()
def extract_pdf(file_path):
with open(file_path, 'rb') as file:
reader = PyPDF2.PdfReader(file)
text = ""
for page in reader.pages:
text += page.extract_text() + "\n"
return text.encode('utf-8').decode('utf-8', 'ignore')
def extract_doc(file_path):
doc = docx.Document(file_path)
full_text = "\n".join([paragraph.text for paragraph in doc.paragraphs])
return full_text.encode('utf-8').decode('utf-8', 'ignore')
def extract_file_content(file_path):
_, file_extension = os.path.splitext(file_path)
if file_extension.lower() == '.txt':
return extract_txt(file_path)
elif file_extension.lower() == '.pdf':
return extract_pdf(file_path)
# elif file_extension.lower() in ['.doc', '.docx']:
# return extract_doc(file_path)
else:
return "error"
import vertexai
from vertexai.generative_models import GenerativeModel, ChatSession
vertexai.init(project="durable-return-430917-b5")
model = GenerativeModel("gemini-1.5-flash-001")
chat = model.start_chat()
def get_chat_response(chat: ChatSession, prompt: str) -> str:
text_response = []
responses = chat.send_message(prompt, stream=True)
for chunk in responses:
text_response.append(chunk.text)
return "".join(text_response)
# def generateEmbeddings(file_path):
# load_and_split_document(file_path)
def process_message(prompt):
# print(prompt)
global multipleFilecontent
multipleFilecontent = ''
print(prompt)
return get_chat_response(chat, prompt)
# return "Output from Gemini API."
messages = [("system", "You are a helpful assistant")]
def respond(history, text_input,files,rag_check):
"""Handles the chat interaction with the language model."""
global messages
global multipleFilecontent
file_names = []
if files:
if isinstance(files, list): # If multiple files are uploaded
file_names = [file.name for file in files]
else: # If a single file is uploaded
file_names = [files.name]
message = {'text':text_input,'files':file_names}
print("Message received:", message)
# Handle file uploads
if message["files"]:
try:
for filepath in message["files"]:
# filepath = message["files"][0]
file_content = extract_file_content(filepath)
if file_content == 'error':
history.append((message["text"], 'Error occur while parsing the file, only pdf,doc and text files allowed.'))
file_content = ''
# return history, gr.MultimodalTextbox(value=None, interactive=True)
return history, gr.update(value=""), gr.update(value=None)
else:
# background_thread = threading.Thread(
# target=generateEmbeddings,
# args=(filepath,), # Pass arguments as a tuple
# daemon=True)
# background_thread.start()
multipleFilecontent += f"Filename : {filepath}, File content: {file_content}\n\n"
except Exception as e:
print(f"Error handling file : {e}")
# Handle text input
if message["text"]:
try:
# Append user input to messages
messages.append(("human", message["text"]))
prompt = message['text']
if multipleFilecontent:
prompt = f"Based on the following content, {multipleFilecontent},\n answer {message['text']}"
file_content = ''
if not rag_check:
context = query_bq(message['text'])
if multipleFilecontent:
prompt = f"Based on the following content, {multipleFilecontent} and {context},\n answer {message['text']}"
else:
prompt = f"Based on the following content, {context},\n answer {message['text']}"
response_content = process_message(prompt)
messages.append(("assistant", response_content))
# Append to chat history for display in the UI
history.append((message["text"], response_content))
except Exception as e:
print(f"Error handling text input: {e}")
else:
if file_names:
files_str = ",".join(os.path.basename(file_names))
history.append(("Files uploaded: {}".format(files_str), "How can I assist with the files?"))
# return history, gr.MultimodalTextbox(value=None, interactive=True)
return history, gr.update(value=""), gr.update(value=None)
# return history, gr.MultimodalTextbox(value=None, interactive=True)
return history, gr.update(value=""), gr.update(value=None)
css = """
.small-file-upload > .file-upload {
height: 30px !important;
line-height: 30px !important;
border-radius: 4px !important;
}
.small-file-upload > .file-upload::before {
height: 28px !important;
line-height: 28px !important;
font-size: 12px !important;
}
.small-file-upload > .file-upload input[type="file"] {
height: 30px !important;
}
"""
with gr.Blocks(css = css,fill_height=True) as demo:
chatbot = gr.Chatbot(
elem_id="chatbot",
bubble_full_width=False,
scale=1,
)
# chat_input = gr.MultimodalTextbox(interactive=True,
# file_count="multiple",
# placeholder="Enter message or upload file...", show_label=False)
# chat_input = gr.MultimodalTextbox(
# interactive=True,
# file_types=[".pdf", ".doc", ".docx", ".txt"],
# file_count="single",
# placeholder="Enter message or upload file...",
# show_label=False
# )
# print(type(chat_input))
# chat_msg = chat_input.submit(respond, [chatbot, chat_input], [chatbot, chat_input])
# chat_msg.then(lambda: gr.MultimodalTextbox(interactive=True), None, [chat_input])
with gr.Row():
with gr.Column(scale=5):
checkbox_input = gr.Checkbox(label="Not related to the domain", scale=1)
text_input = gr.Textbox(placeholder="Enter message...", show_label=False)
with gr.Column(scale = 1):
file_input = gr.File(
file_count="multiple",
file_types=[".pdf", ".txt"],
label="Upload files",
scale=1,
elem_classes="small-file-upload"
)
chat_msg = gr.Button("Submit")
chat_msg.click(respond, [chatbot, text_input,file_input,checkbox_input], [chatbot, text_input,file_input])#{'text':text_input,'files':file_input}
demo.queue()
demo.launch(share=True)