carbonnnnn commited on
Commit
7117f9e
1 Parent(s): be5df66

update IGL

Browse files
README.md CHANGED
@@ -5,6 +5,6 @@ colorFrom: red
5
  colorTo: indigo
6
  sdk: gradio
7
  sdk_version: 4.42.0
8
- app_file: app_drive.py
9
  pinned: false
10
  ---
 
5
  colorTo: indigo
6
  sdk: gradio
7
  sdk_version: 4.42.0
8
+ app_file: app_temp.py
9
  pinned: false
10
  ---
app.py CHANGED
@@ -6,7 +6,6 @@ import datetime
6
  import os
7
 
8
  from lc_base.chain import openai_chain
9
- from driveapi.drive import upload_chat_to_drive
10
 
11
  # global time_diff, model_name, search_type
12
  time_diff = 0
@@ -51,22 +50,6 @@ def respond(message, chat_history):
51
  time.sleep(2)
52
  return " ", chat_history
53
 
54
- def save_feedback(feedback):
55
- global user_feedback
56
- user_feedback = feedback
57
-
58
- curr_date = datetime.datetime.now()
59
- file_name = f"chat_{curr_date.day}_{curr_date.month}_{curr_date.hour}_{curr_date.minute}.csv"
60
- log_data = [
61
- ["Question", "Response", "Model", "Time", "Feedback"],
62
- [input_question, model_response, model_name, time_diff, user_feedback]
63
- ]
64
-
65
- if user_feedback == "Yes" or feedback == "No":
66
- upload_chat_to_drive(log_data, file_name)
67
-
68
- def default_feedback():
69
- return "🤔"
70
 
71
  with gr.Blocks(theme=gr.themes.Soft(primary_hue="emerald", neutral_hue="slate")) as chat:
72
  gr.HTML(title)
@@ -87,22 +70,9 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="emerald", neutral_hue="slate"))
87
  ], inputs=msg, label= "Click on any example to copy in the chatbox"
88
  )
89
 
90
- with gr.Column():
91
- feedback_radio = gr.Radio(
92
- choices=["Yes", "No", "🤔"],
93
- value=["🤔"],
94
- label="Did you like the latest response?",
95
- info="Selecting Yes/No will send the following diagnostic data - Question, Response, Time Taken",
96
- )
97
 
98
  msg.submit(respond, [msg, chatbot], [msg, chatbot])
99
- msg.submit(default_feedback, outputs=[feedback_radio])
100
-
101
 
102
- feedback_radio.change(
103
- fn=save_feedback,
104
- inputs=[feedback_radio]
105
- )
106
 
107
  gr.HTML(description)
108
 
 
6
  import os
7
 
8
  from lc_base.chain import openai_chain
 
9
 
10
  # global time_diff, model_name, search_type
11
  time_diff = 0
 
50
  time.sleep(2)
51
  return " ", chat_history
52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
 
54
  with gr.Blocks(theme=gr.themes.Soft(primary_hue="emerald", neutral_hue="slate")) as chat:
55
  gr.HTML(title)
 
70
  ], inputs=msg, label= "Click on any example to copy in the chatbox"
71
  )
72
 
 
 
 
 
 
 
 
73
 
74
  msg.submit(respond, [msg, chatbot], [msg, chatbot])
 
 
75
 
 
 
 
 
76
 
77
  gr.HTML(description)
78
 
app_temp.py ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Application file for Gradio App for OpenAI Model
2
+
3
+ import gradio as gr
4
+ import time
5
+ import datetime
6
+ import os
7
+
8
+ from lc_base.chain import openai_chain
9
+ from lc_base.dnd_database import create_dnd_database
10
+
11
+ ############################# Global Params #############################
12
+
13
+ time_diff = 0
14
+ # model_name="gpt-3.5-turbo-1106" # FOR TESTING
15
+ # model_name = "gpt-4-1106-preview"
16
+ model_name = "gpt-4o-mini-2024-07-18"
17
+ search_type = "stuff"
18
+ input_question = ""
19
+ model_response = ""
20
+ user_feedback = ""
21
+
22
+ dir = ""
23
+ title = """<h1 align="center">ResearchBuddy</h1>"""
24
+ description = """<br><br><h3 align="center">This is a GPT based Research Buddy to assist in navigating new research topics.</h3>"""
25
+
26
+ DEFAULT_STATUS = "⬆Upload PDF files"
27
+
28
+
29
+ ############################# Drag and Drop PDF processing #############################
30
+ def check_pdfs(pdf_files):
31
+ global db
32
+ db = create_dnd_database(pdf_files)
33
+ if not db:
34
+ return "Please upload PDF files again or submit a drive link containing only PDFs."
35
+ else:
36
+ return "Processing Completed - You can start the chat now!"
37
+
38
+ ############################# Chatbot Specific functions #############################
39
+ def user(user_message, history):
40
+ return "", history + [[user_message, None]]
41
+
42
+ def respond(message, chat_history):
43
+
44
+ global time_diff, model_response, input_question
45
+
46
+ question = str(message)
47
+ chain = openai_chain(inp_dir=dir)
48
+
49
+ query = question
50
+ start_time = time.time()
51
+
52
+ output = chain.get_response_from_drive(query=query, database=db, k=10, model_name=model_name, type=search_type)
53
+
54
+ # Update global variables for logging
55
+ time_diff = time.time() - start_time
56
+ model_response = output
57
+ input_question = question
58
+ bot_message = output
59
+ chat_history.append((message, bot_message))
60
+
61
+ time.sleep(1) # Pause for a second to avoid overloading
62
+ return " ", chat_history
63
+
64
+
65
+ ############################# Gradio Application Block #############################
66
+ with gr.Blocks(theme=gr.themes.Soft(primary_hue="emerald", neutral_hue="slate")) as chat:
67
+ gr.HTML(title)
68
+
69
+ global db
70
+
71
+ # PDF Drag and Drop + Drive link Input + Status containers
72
+ with gr.Row(equal_height=True):
73
+ with gr.Column():
74
+ with gr.Row():
75
+ pdf_files_dnd = gr.File(file_count='multiple', height=250, label="Upload PDF Files")
76
+ with gr.Row():
77
+ status_message = gr.Text(label="Status", value=DEFAULT_STATUS, text_align='center')
78
+
79
+
80
+
81
+ pdf_files_dnd.change(
82
+ fn=check_pdfs,
83
+ inputs=[pdf_files_dnd],
84
+ outputs=[status_message],
85
+ preprocess=False,
86
+ postprocess=False) # Set preprocess and postprocess to False, to avoid the tmpfile object creation, instead get a Dict
87
+
88
+ # Chatbot container
89
+ chatbot = gr.Chatbot(height=750)
90
+ msg = gr.Textbox(label="Send a message", placeholder="Send a message",
91
+ show_label=False, container=False)
92
+
93
+ with gr.Row():
94
+ with gr.Column():
95
+ clear_history_button = gr.ClearButton(value="Clear Chat History")
96
+
97
+ with gr.Column():
98
+ new_chat_button = gr.ClearButton(value="New Chat")
99
+
100
+ # Sample questions
101
+ with gr.Row():
102
+ with gr.Column():
103
+ gr.Examples([
104
+ ["Explain these documents to me in simpler terms."],
105
+ ["What does these documents talk about?"],
106
+ ["Give the key topics covered in these documents in less than 10 words."],
107
+ ["What are the key findings in these documents?"],
108
+ ], inputs=msg, label= "Click on any example to copy in the chatbox"
109
+ )
110
+
111
+
112
+ # Get a response when a message is submitted to the chatbot
113
+ msg.submit(
114
+ fn = respond,
115
+ inputs = [msg, chatbot],
116
+ outputs = [msg, chatbot],
117
+ queue = True)
118
+
119
+
120
+ # Clear the chat history/ New chat
121
+ clear_history_button.click(lambda: [None, None], outputs=[msg, chatbot])
122
+ new_chat_button.click(
123
+ lambda: [None, None, None, None, DEFAULT_STATUS],
124
+ outputs=[msg, chatbot, pdf_files_dnd, status_message])
125
+
126
+ # Description at the bottom of the application
127
+ gr.HTML(description)
128
+
129
+ # Enable queing
130
+ chat.queue()
131
+ chat.launch()
driveapi/__pycache__/drive.cpython-310.pyc ADDED
Binary file (2.25 kB). View file
 
driveapi/__pycache__/service.cpython-310.pyc ADDED
Binary file (848 Bytes). View file
 
lc_base/__pycache__/chain.cpython-310.pyc CHANGED
Binary files a/lc_base/__pycache__/chain.cpython-310.pyc and b/lc_base/__pycache__/chain.cpython-310.pyc differ
 
lc_base/__pycache__/dnd_database.cpython-310.pyc ADDED
Binary file (1.15 kB). View file
 
lc_base/dnd_database.py CHANGED
@@ -1,8 +1,22 @@
1
- from driveapi.drive import process_pdf
2
-
3
  from langchain.embeddings.openai import OpenAIEmbeddings
4
  from langchain.text_splitter import CharacterTextSplitter
5
  from langchain.vectorstores import FAISS
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  def create_dnd_database(file_list):
8
  raw_text = ''
 
 
 
1
  from langchain.embeddings.openai import OpenAIEmbeddings
2
  from langchain.text_splitter import CharacterTextSplitter
3
  from langchain.vectorstores import FAISS
4
+ import PyPDF2
5
+
6
+
7
+ # Function to process a PDF file
8
+ def process_pdf(file_stream):
9
+ if isinstance(file_stream, dict): # Check if PDF was obtained using Drag and Drop or Drive link
10
+ file_path = file_stream['name'] # Use 'path' for local testing and 'name' for Gradio
11
+ pdf_reader = PyPDF2.PdfReader(file_path)
12
+ else:
13
+ pdf_reader = PyPDF2.PdfReader(file_stream)
14
+ text = ""
15
+ for page_num in range(len(pdf_reader.pages)):
16
+ page = pdf_reader.pages[page_num]
17
+ text += page.extract_text()
18
+ return text
19
+
20
 
21
  def create_dnd_database(file_list):
22
  raw_text = ''