shukdevdattaEX commited on
Commit
7bc10be
·
verified ·
1 Parent(s): 1f6dd9c

Create v1.txt

Browse files
Files changed (1) hide show
  1. v1.txt +251 -0
v1.txt ADDED
@@ -0,0 +1,251 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from llama_index.core import VectorStoreIndex, Document
3
+ from llama_index.llms.openai import OpenAI
4
+ from llama_index.core import Settings
5
+ import os
6
+ import pdfplumber
7
+ from docx import Document as DocxDocument
8
+ import json
9
+ from datetime import datetime
10
+
11
+ # Global variables
12
+ chat_engine = None
13
+ conversation_history = []
14
+
15
+ # Function to read PDF files
16
+ def read_pdf(file_path):
17
+ with pdfplumber.open(file_path) as pdf:
18
+ text = ''
19
+ for page in pdf.pages:
20
+ text += page.extract_text() + '\n'
21
+ return text
22
+
23
+ # Function to read DOCX files
24
+ def read_docx(file_path):
25
+ doc = DocxDocument(file_path)
26
+ text = ''
27
+ for paragraph in doc.paragraphs:
28
+ text += paragraph.text + '\n'
29
+ return text
30
+
31
+ # Function to load and index documents
32
+ def load_data(files, api_key):
33
+ global chat_engine
34
+
35
+ if not files or not api_key:
36
+ return "Please provide both API key and files to proceed."
37
+
38
+ try:
39
+ docs = []
40
+ for file in files:
41
+ if file.name.endswith('.pdf'):
42
+ text = read_pdf(file.name)
43
+ docs.append(Document(text=text))
44
+ elif file.name.endswith('.docx'):
45
+ text = read_docx(file.name)
46
+ docs.append(Document(text=text))
47
+
48
+ # Set OpenAI API key
49
+ os.environ["OPENAI_API_KEY"] = api_key
50
+
51
+ Settings.llm = OpenAI(
52
+ model="gpt-3.5-turbo",
53
+ temperature=0.5,
54
+ api_key=api_key,
55
+ system_prompt="You are an expert on the Streamlit Python library and your job is to answer technical questions. Assume that all questions are related to the Streamlit Python library. Keep your answers technical and based on facts – do not hallucinate features."
56
+ )
57
+
58
+ index = VectorStoreIndex.from_documents(docs)
59
+ chat_engine = index.as_chat_engine(chat_mode="condense_question", verbose=True)
60
+
61
+ return "Documents loaded and indexed successfully! You can now start chatting."
62
+ except Exception as e:
63
+ return f"Error loading documents: {str(e)}"
64
+
65
+ # Function to handle chat
66
+ def chat_with_docs(message, history, api_key):
67
+ global chat_engine, conversation_history
68
+
69
+ if not api_key:
70
+ return history + [{"role": "assistant", "content": "Please enter your OpenAI API key first."}]
71
+
72
+ if chat_engine is None:
73
+ return history + [
74
+ {"role": "user", "content": message},
75
+ {"role": "assistant", "content": "Please upload and load documents first before asking questions."}
76
+ ]
77
+
78
+ try:
79
+ response = chat_engine.chat(message)
80
+ conversation_history.append({"role": "user", "content": message})
81
+ conversation_history.append({"role": "assistant", "content": response.response})
82
+
83
+ return history + [
84
+ {"role": "user", "content": message},
85
+ {"role": "assistant", "content": response.response}
86
+ ]
87
+ except Exception as e:
88
+ return history + [
89
+ {"role": "user", "content": message},
90
+ {"role": "assistant", "content": f"Error: {str(e)}"}
91
+ ]
92
+
93
+ # Function to save conversation
94
+ def save_conversation():
95
+ global conversation_history
96
+
97
+ if not conversation_history:
98
+ return "No conversation to save."
99
+
100
+ try:
101
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
102
+ with open("conversations.json", "a") as f:
103
+ conv_data = {
104
+ "timestamp": timestamp,
105
+ "messages": conversation_history
106
+ }
107
+ json.dump(conv_data, f)
108
+ f.write("\n")
109
+ return "Conversation saved successfully!"
110
+ except Exception as e:
111
+ return f"Error saving conversation: {str(e)}"
112
+
113
+ # Function to load previous conversations
114
+ def load_conversations():
115
+ if os.path.exists("conversations.json"):
116
+ try:
117
+ with open("conversations.json", "r") as f:
118
+ conversations = [json.loads(line) for line in f]
119
+
120
+ conv_text = ""
121
+ for i, conv in enumerate(conversations):
122
+ conv_text += f"\n{'='*50}\nConversation {i + 1}\n{'='*50}\n"
123
+ timestamp = conv.get("timestamp", "Unknown time")
124
+ conv_text += f"Timestamp: {timestamp}\n\n"
125
+
126
+ messages = conv.get("messages", conv) # Handle old format
127
+ for message in messages:
128
+ role = message.get('role', 'unknown')
129
+ content = message.get('content', '')
130
+ conv_text += f"{role.upper()}: {content}\n\n"
131
+
132
+ return conv_text if conv_text else "No previous conversations found."
133
+ except Exception as e:
134
+ return f"Error loading conversations: {str(e)}"
135
+ return "No previous conversations found."
136
+
137
+ # Function to clear current conversation
138
+ def clear_conversation():
139
+ global conversation_history
140
+ conversation_history = []
141
+ return []
142
+
143
+ # Function to delete all conversations
144
+ def delete_all_conversations():
145
+ try:
146
+ if os.path.exists("conversations.json"):
147
+ os.remove("conversations.json")
148
+ return "All conversations deleted successfully!"
149
+ return "No conversations to delete."
150
+ except Exception as e:
151
+ return f"Error deleting conversations: {str(e)}"
152
+
153
+ # Create Gradio interface
154
+ with gr.Blocks(title="Chat with Documents 💬 📚") as demo:
155
+ gr.Markdown("# Chat with Documents 💬 📚")
156
+ gr.Markdown("Upload PDF or DOCX files and chat with them using AI!")
157
+
158
+ with gr.Row():
159
+ with gr.Column(scale=2):
160
+ api_key_input = gr.Textbox(
161
+ label="OpenAI API Key",
162
+ type="password",
163
+ placeholder="Enter your OpenAI API key here..."
164
+ )
165
+
166
+ file_upload = gr.File(
167
+ label="Upload PDF or DOCX files",
168
+ file_count="multiple",
169
+ file_types=[".pdf", ".docx"]
170
+ )
171
+
172
+ load_btn = gr.Button("Load Documents", variant="primary")
173
+ load_status = gr.Textbox(label="Status", interactive=False)
174
+
175
+ load_btn.click(
176
+ fn=load_data,
177
+ inputs=[file_upload, api_key_input],
178
+ outputs=load_status
179
+ )
180
+
181
+ with gr.Row():
182
+ with gr.Column(scale=3):
183
+ chatbot = gr.Chatbot(
184
+ label="Chat",
185
+ height=400
186
+ )
187
+ msg = gr.Textbox(
188
+ label="Your Question",
189
+ placeholder="Ask a question about your documents..."
190
+ )
191
+
192
+ with gr.Row():
193
+ submit_btn = gr.Button("Send", variant="primary")
194
+ clear_btn = gr.Button("Clear Chat")
195
+
196
+ with gr.Row():
197
+ save_btn = gr.Button("Save Conversation")
198
+ save_status = gr.Textbox(label="Save Status", interactive=False)
199
+
200
+ with gr.Column(scale=1):
201
+ gr.Markdown("### Previous Conversations")
202
+ load_convs_btn = gr.Button("Load Previous Conversations")
203
+ convs_display = gr.Textbox(
204
+ label="Conversation History",
205
+ lines=20,
206
+ interactive=False
207
+ )
208
+ delete_all_btn = gr.Button("Delete All Conversations", variant="stop")
209
+ delete_status = gr.Textbox(label="Delete Status", interactive=False)
210
+
211
+ # Event handlers
212
+ submit_btn.click(
213
+ fn=chat_with_docs,
214
+ inputs=[msg, chatbot, api_key_input],
215
+ outputs=chatbot
216
+ ).then(
217
+ lambda: "",
218
+ outputs=msg
219
+ )
220
+
221
+ msg.submit(
222
+ fn=chat_with_docs,
223
+ inputs=[msg, chatbot, api_key_input],
224
+ outputs=chatbot
225
+ ).then(
226
+ lambda: "",
227
+ outputs=msg
228
+ )
229
+
230
+ clear_btn.click(
231
+ fn=clear_conversation,
232
+ outputs=chatbot
233
+ )
234
+
235
+ save_btn.click(
236
+ fn=save_conversation,
237
+ outputs=save_status
238
+ )
239
+
240
+ load_convs_btn.click(
241
+ fn=load_conversations,
242
+ outputs=convs_display
243
+ )
244
+
245
+ delete_all_btn.click(
246
+ fn=delete_all_conversations,
247
+ outputs=delete_status
248
+ )
249
+
250
+ if __name__ == "__main__":
251
+ demo.launch()