Redmind commited on
Commit
0e9a631
·
verified ·
1 Parent(s): e4fcaa6

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -1440
app.py DELETED
@@ -1,1440 +0,0 @@
1
- from PIL import Image
2
- import base64
3
- from io import BytesIO
4
- import os
5
- import re
6
- import tempfile
7
- import wave
8
- import requests
9
- import gradio as gr
10
- import time
11
- import shutil
12
- import json
13
- import nltk
14
- import mysql.connector
15
- import fnmatch
16
- # audio related code is not included based on Arun's input
17
- # audio package
18
- import speech_recognition as sr
19
- from pydub import AudioSegment
20
- from pydub.playback import play
21
- # SMTP code is not included since HFSpaces doesn't support it
22
- # email library
23
- import smtplib, ssl
24
- from email.mime.multipart import MIMEMultipart
25
- from email.mime.text import MIMEText
26
- from email.mime.base import MIMEBase
27
- from email import encoders
28
- # langchain
29
- from langchain_core.prompts import ChatPromptTemplate
30
- from langchain_core.output_parsers import StrOutputParser
31
- from langchain_core.runnables import RunnableSequence, RunnableLambda
32
- from langchain_openai import ChatOpenAI
33
- from langchain_openai import OpenAIEmbeddings
34
- from langchain_community.vectorstores import FAISS
35
- from langchain_community.utilities import SQLDatabase
36
- from langchain.agents import create_tool_calling_agent, AgentExecutor, Tool
37
- from langchain.text_splitter import RecursiveCharacterTextSplitter
38
- from langchain.tools import StructuredTool
39
- #from langchain.pydantic_v1 import BaseModel, Field
40
- from pydantic import BaseModel, Field
41
- from PyPDF2 import PdfReader
42
- from nltk.tokenize import sent_tokenize
43
- from datetime import datetime
44
- from sqlalchemy import create_engine
45
- from sqlalchemy.sql import text
46
- import openai
47
-
48
- # pandas
49
- import pandas as pd
50
- from pandasai.llm.openai import OpenAI
51
- from pandasai import SmartDataframe
52
- from dotenv import load_dotenv
53
-
54
- # Load environment variables
55
- load_dotenv()
56
-
57
- # langfuse analytics
58
- from langfuse.callback import CallbackHandler
59
-
60
- # Inventory API data table
61
- from tabulate import tabulate
62
-
63
- #forcefully stop the agent execution
64
- import concurrent.futures
65
- import threading
66
-
67
- # mailjet_rest to send email
68
- from mailjet_rest import Client
69
- import base64
70
-
71
- #for PDF form filling
72
- from PyPDFForm import FormWrapper
73
-
74
- #Variables Initialization
75
- agent_executor = None
76
- vector_store1 = None
77
- texts1 = None
78
- excel_dataframe = None
79
- file_extension = None
80
- total_rows = ""
81
- docstatus = ""
82
- sample_table = ""
83
- #This is to define the summary of the runtime tool. This summary will be updated in prompt template and description of the new tool
84
- run_time_tool_summary=""
85
-
86
- # Define global variables for managing the thread and current_event
87
- executor = concurrent.futures.ThreadPoolExecutor(max_workers=1)
88
-
89
- current_event = None
90
- stop_event = threading.Event()
91
-
92
- # LangFuse API keys and host settings
93
- os.environ["LANGFUSE_PUBLIC_KEY"] = os.getenv("LANGFUSE_PUBLIC_KEY")
94
- os.environ["LANGFUSE_SECRET_KEY"] = os.getenv("LANGFUSE_SECRET_KEY")
95
- os.environ["LANGFUSE_HOST"] = os.getenv("LANGFUSE_HOST")
96
-
97
- DB_USER = 'u852023448_redmindgpt'
98
- DB_PASSWORD = 'redmindGpt@123'
99
- DB_HOST = '217.21.88.10'
100
- DB_NAME = 'u852023448_redmindgpt'
101
-
102
-
103
- langfuse_handler = CallbackHandler()
104
- langfuse_handler.auth_check() # Optional: Checks if the authentication is successful
105
-
106
- nltk.download('punkt')
107
-
108
- open_api_key_token = os.getenv("OPEN_AI_API")
109
-
110
- os.environ['OPENAI_API_KEY'] = open_api_key_token
111
- pdf_path = "Inbound.pdf"
112
-
113
- db_uri = os.getenv("POSTGRESQL_CONNECTION")
114
-
115
- # Database setup
116
- db = SQLDatabase.from_uri(db_uri)
117
-
118
- user_email = ""
119
- warehouse_name = ""
120
- warehouse_id = ""
121
- # Today's date to be populated in inventory API
122
- inventory_date = datetime.today().strftime('%Y-%m-%d')
123
-
124
- apis = [
125
- # fetch warehouse ID
126
- {
127
- "url": "http://193.203.162.39:8383/nxt-wms/userWarehouse/fetchWarehouseForUserId?",
128
- "params": {"query": warehouse_name, "userId": 164}
129
- },
130
-
131
- # Stock summary based on warehouse id
132
- {
133
- "url": "http://193.203.162.39:8383/nxt-wms/transactionHistory/stockSummary?",
134
- "params": {"branchId": 343, "onDate": inventory_date, "warehouseId": warehouse_id}
135
- }
136
- ]
137
-
138
- # LLM setup
139
- llm = ChatOpenAI(model="gpt-4o-mini", max_tokens=300, temperature=0.1)
140
- llm_chart = OpenAI(is_safe=False)
141
-
142
- def get_schema(_):
143
- schema_info = db.get_table_info() # This should be a string of your SQL schema
144
- return schema_info
145
-
146
-
147
- def generate_sql_query(question):
148
- schema = get_schema(None)
149
- template_query_generation = """
150
- Schema: {schema}
151
- Question: {question}
152
- Provide a SQL query to answer the above question using the exact field names and table names specified in the schema.
153
- SQL Query (Please provide only the SQL statement without explanations or formatting):
154
- """
155
- prompt_query_generation = ChatPromptTemplate.from_template(template_query_generation)
156
- schema_and_question = RunnableLambda(lambda _: {'schema': schema, 'question': question})
157
- sql_chain = RunnableSequence(
158
- schema_and_question,
159
- prompt_query_generation,
160
- llm.bind(stop=["SQL Query End"]), # Adjust the stop sequence to your need
161
- StrOutputParser()
162
- )
163
- sql_query = sql_chain.invoke({})
164
- sql_query = sql_chain.invoke({}, config={"callbacks": [langfuse_handler]})
165
- return sql_query.strip()
166
-
167
-
168
- def run_query(query):
169
- # Clean the query by removing markdown symbols and trimming whitespace
170
- clean_query = query.replace("```sql", "").replace("```", "").strip()
171
- print(f"Executing SQL Query: {clean_query}")
172
- try:
173
- result = db.run(clean_query)
174
- return result
175
- except Exception as e:
176
- print(f"Error executing query: {e}")
177
- return None
178
-
179
-
180
- # Define the database query tool
181
- # The function that uses the above models
182
- # Define the function that will handle the database query
183
- def database_tool(question):
184
- sql_query = generate_sql_query(question)
185
- return run_query(sql_query)
186
-
187
-
188
- def get_ASN_data(question):
189
- base_url = os.getenv("ASN_API_URL")
190
- print(f"base_url{base_url}")
191
- complete_url = f"{base_url}branchMaster.id=343&transactionUid={question}&userId=164&transactionType=ASN"
192
- try:
193
- response = requests.get(complete_url)
194
- print(f"complete_url{complete_url}")
195
- print(f"response{response}")
196
- data = response.json()
197
- response.raise_for_status()
198
-
199
- if 'result' in data and 'content' in data['result'] and data['result']['content']:
200
- content = data['result']['content'][0]
201
- trnHeaderAsn = content['trnHeaderAsn']
202
- party = content['party'][0]
203
-
204
- transactionUid = trnHeaderAsn['transactionUid']
205
- customerOrderNo = trnHeaderAsn.get('customerOrderNo', 'N/A')
206
- orderDate = trnHeaderAsn.get('orderDate', 'N/A')
207
- customerInvoiceNo = trnHeaderAsn.get('customerInvoiceNo', 'N/A')
208
- invoiceDate = trnHeaderAsn.get('invoiceDate', 'N/A')
209
- expectedReceivingDate = trnHeaderAsn['expectedReceivingDate']
210
- transactionStatus = trnHeaderAsn['transactionStatus']
211
- shipper_code = party['shipper']['code'] if party['shipper'] else 'N/A'
212
- shipper_name = party['shipper']['name'] if party['shipper'] else 'N/A'
213
-
214
- data = [
215
- ["Transaction UID", transactionUid],
216
- ["Customer Order No", customerOrderNo],
217
- ["Order Date", orderDate],
218
- ["Customer Invoice No", customerInvoiceNo],
219
- ["Invoice Date", invoiceDate],
220
- ["Expected Receiving Date", expectedReceivingDate],
221
- ["Transaction Status", transactionStatus],
222
- ["Shipper Code", shipper_code],
223
- ["Shipper Name", shipper_name]
224
- ]
225
- return f"The ASN details of {question} is {data}."
226
- else:
227
- return "ASN Details are not found. Please contact system administrator."
228
-
229
- except requests.exceptions.HTTPError as http_err:
230
- print(f"HTTP error occurred: {http_err}")
231
- except Exception as err:
232
- print(f"An error occurred: {err}")
233
-
234
- def load_and_split_pdf(pdf_path):
235
- reader = PdfReader(pdf_path)
236
- text = ''
237
- for page in reader.pages:
238
- text += page.extract_text()
239
- text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=50)
240
- texts = text_splitter.split_text(text)
241
- return texts
242
-
243
-
244
- def create_vector_store(texts):
245
- embeddings = OpenAIEmbeddings()
246
- vector_store = FAISS.from_texts(texts, embeddings)
247
- return vector_store
248
-
249
-
250
- def query_vector_store(vector_store, query, config=None):
251
- if config:
252
- print("Config passed:", config)
253
- docs = vector_store.similarity_search(query, k=5)
254
- print(f"Vector store return: {docs}")
255
- return docs
256
-
257
-
258
- def summarize_document(docs):
259
- summarized_docs = []
260
- for doc in docs:
261
- if isinstance(doc, list):
262
- doc_content = ' '.join([d.page_content for d in doc])
263
- else:
264
- doc_content = doc.page_content
265
-
266
- sentences = sent_tokenize(doc_content)
267
- if len(sentences) > 5:
268
- summarized_content = ' '.join(sentences[:5])
269
- else:
270
- summarized_content = doc_content
271
- summarized_docs.append(summarized_content)
272
- return '\n\n'.join(summarized_docs)
273
-
274
-
275
- texts = load_and_split_pdf(pdf_path)
276
- vector_store = create_vector_store(texts)
277
-
278
- def document_data_tool_runtime(question):
279
- print(f"Document data runtime tool enter: {question} with {vector_store1}")
280
- query_response = query_vector_store(vector_store1, question, config={"callbacks": [langfuse_handler]})
281
- return query_response
282
-
283
- def document_data_tool(question):
284
- print(f"Document data tool enter: {question}")
285
- # query_string = question['tags'][0] if 'tags' in question and question['tags'] else ""
286
- query_response = query_vector_store(vector_store, question, config={"callbacks": [langfuse_handler]})
287
- # summarized_response = summarize_document(query_response)
288
- return query_response
289
-
290
- # mailjet API since SMTP is not supported HF spaces
291
- def send_email_with_attachment_mailjet(recipient_email, subject, body, attach_img_base64=None):
292
- api_key = os.getenv("MAILJET_API_KEY")
293
- api_secret = os.getenv("MAILJET_API_SECRET")
294
-
295
- # Initialize the Mailjet client
296
- mailjet = Client(auth=(api_key, api_secret), version='v3.1')
297
-
298
- # Define the email details with an attachment
299
- data = {
300
- 'Messages': [
301
- {
302
- "From": {
303
- "Email": "[email protected]",
304
- "Name": "Redmind Technologies"
305
- },
306
- "To": [
307
- {
308
- "Email": recipient_email,
309
- "Name": ""
310
- }
311
- ],
312
- "Subject": subject,
313
- "TextPart": body,
314
-
315
- "CustomID": "AppGettingStartedTest",
316
- "Attachments": [
317
- {
318
- "ContentType": "image/png", # Replace with the correct MIME type of your image
319
- "Filename": "inventory_report.png", # Name of the image as it will appear in the email
320
- "Base64Content": attach_img_base64 # Base64-encoded image content
321
- }
322
- ]
323
-
324
- }
325
- ]
326
- }
327
-
328
- # Send the email
329
- result = mailjet.send.create(data=data)
330
-
331
- # Check if the email was sent successfully
332
- if result.status_code == 200:
333
- print("Email sent successfully with attachment!")
334
- else:
335
- print(f"Failed to send email. Status code: {result.status_code}")
336
- print(result.json())
337
-
338
-
339
- #smtp lib
340
- def send_email_with_attachment(recipient_email, subject, body, attachment_path):
341
- try:
342
- sender_email = os.getenv("EMAIL_SENDER")
343
- sender_password = os.getenv("EMAIL_PASSWORD")
344
- # Create a multipart message
345
- msg = MIMEMultipart()
346
- msg['From'] = sender_email
347
- msg['To'] = recipient_email
348
- msg['Subject'] = subject
349
- # Attach the body with the msg instance
350
- msg.attach(MIMEText(body, 'plain'))
351
- # Open the file to be sent
352
- attachment = open(attachment_path, "rb")
353
- # print("Attached the image")
354
- # Instance of MIMEBase and named as p
355
- part = MIMEBase('application', 'octet-stream')
356
-
357
- # To change the payload into encoded form
358
- part.set_payload((attachment).read())
359
-
360
- # Encode into base64
361
- encoders.encode_base64(part)
362
-
363
- part.add_header('Content-Disposition', f"attachment; filename= {attachment_path}")
364
-
365
- # Attach the instance 'part' to instance 'msg'
366
- msg.attach(part)
367
-
368
- server = smtplib.SMTP('smtp.gmail.com', 587)
369
- server.starttls()
370
- server.login(sender_email, sender_password)
371
- text = msg.as_string()
372
- server.sendmail(sender_email, recipient_email, text)
373
- server.quit()
374
-
375
- except Exception as error:
376
- print(f"An error occurred: {error}")
377
-
378
- # return 1
379
-
380
-
381
- def make_api_request(url, params):
382
- """Generic function to make API GET requests and return JSON data."""
383
- try:
384
- response = requests.get(url, params=params)
385
- response.raise_for_status() # Raises an HTTPError if the response was an error
386
- return response.json() # Return the parsed JSON data
387
- except requests.exceptions.HTTPError as http_err:
388
- print(f"HTTP error occurred: {http_err}")
389
- except Exception as err:
390
- print(f"An error occurred: {err}")
391
-
392
-
393
- def inventory_report(question):
394
- # Split the question to extract warehouse name, user question, and optional email
395
- if question.count(":") > 0:
396
- parts = question.split(":", 2)
397
- warehouse_name= parts[0].strip()
398
- user_question = parts[1].strip()
399
- user_email = parts[2].strip() if len(parts) > 2 else None
400
- print(f"Warehouse: {warehouse_name}, Email: {user_email}, Question: {user_question}")
401
- else:
402
- return "warehouse name not found"
403
-
404
- data = make_api_request(apis[0]["url"], apis[0]["params"])
405
- print(data)
406
- if data:
407
- # Extracting the id for the warehouse with the name "WH"
408
- warehouse_id = next((item['id'] for item in data['result'] if item['wareHouseId'] == warehouse_name), None)
409
-
410
- if (warehouse_id):
411
-
412
- # Step 3: Update the placeholder with the actual warehouse_id
413
- for api in apis:
414
- if "warehouseId" in api["params"]:
415
- api["params"]["warehouseId"] = warehouse_id
416
-
417
- data1 = make_api_request(apis[1]["url"], apis[1]["params"])
418
- if (data1):
419
- headers = ["S.No", "Warehouse Code", "Warehouse Name", "Customer Code", "Customer Name", "Item Code", "Item Name",
420
- "Currency", "EAN", "UOM", "Quantity", "Gross Weight", "Volume", "Total Value"]
421
- table_data = []
422
-
423
- for index, item in enumerate(data1['result'], start=1):
424
- row = [
425
- index, # Serial number
426
- item['warehouse']['code'],
427
- item['warehouse']['name'],
428
- item['customer']['code'],
429
- item['customer']['name'],
430
- item['skuMaster']['code'],
431
- item['skuMaster']['name'],
432
- item['currency']['code'],
433
- item['eanUpc'],
434
- item['uom']['code'],
435
- item['totalQty'],
436
- item['grossWeight'],
437
- item['volume'],
438
- item['totalValue']
439
- ]
440
- table_data.append(row)
441
-
442
- # Convert to pandas DataFrame
443
- df = pd.DataFrame(table_data, columns=headers)
444
-
445
- chart_link = chat_with_llm(df,question)
446
-
447
- return chart_link
448
- else:
449
- return "There are no inventory details for the warehouse you have given."
450
- else:
451
- return "Please provide a warehouse name available in the database."
452
-
453
- def chat_with_llm(df,question):
454
- sdf = SmartDataframe(df, config={"llm": llm_chart})
455
- llm_response = sdf.chat(question)
456
- return llm_response
457
-
458
- def bind_llm(llm, tools,prompt_template):
459
- llm = llm.bind()
460
- agent = create_tool_calling_agent(llm, tools, ChatPromptTemplate.from_template(prompt_template))
461
- agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
462
- return agent_executor
463
-
464
- # Define input and output models using Pydantic
465
- class QueryInput(BaseModel):
466
- question: str = Field(
467
- description="The question to be answered by appropriate tool. Please follow the instructions. For API tool, do not send the question as it is. Please send the ASN id.")# Invoke datavisulaization tool by processing the user question and send two inputs to the tool. One input will be the warehouse name and another input to the tool will be the entire user_question itself. Please join those two strings and send them as a single input string with ':' as delimiter")
468
- # config: dict = Field(default={}, description="Optional configuration for the database query.")
469
-
470
-
471
- # Define the output model for database queries
472
- class QueryOutput(BaseModel):
473
- result: str = Field(...,
474
- description="Display the answer based on the prompts given in each tool. For dataVisualization tool, it sends a image file as output. Please give the image file path only to the gr.Image. For DocumentData tool, Please provide a complete and concise response within 200 words and Ensure that the response is not truncated and covers the essential points.")
475
-
476
-
477
- # Wrap the function with StructuredTool for better parameter handling
478
- tools = [
479
-
480
- StructuredTool(
481
- func=get_ASN_data,
482
- name="APIData",
483
- args_schema=QueryInput,
484
- output_schema=QueryOutput,
485
- description="Tool to get details of ASN api. ASN id will be in the input with the format of first three letters as ASN and it is followed by 11 digit numeral. Pass only the id as input. Do not send the complete user question to the tool. If there are any other queries related to ASN without ASN id, please use the document tool."
486
- ),
487
- StructuredTool(
488
- func=document_data_tool,
489
- name="DocumentData",
490
- args_schema=QueryInput,
491
- output_schema=QueryOutput,
492
- description="You are an AI assistant trained to help with warehouse management questions based on a detailed document about our WMS. The document covers various processes such as ASN handling, purchase orders, cross docking, appointment scheduling for shipments, and yard management. Please provide a complete and concise response within 200 words and Ensure that the response is not truncated and covers the essential points. "
493
- ),
494
- StructuredTool(
495
- func=database_tool,
496
- name="DatabaseQuery",
497
- args_schema=QueryInput,
498
- output_schema=QueryOutput,
499
- description="Tool to query the database based on structured input."
500
- ),
501
- StructuredTool(
502
- func=inventory_report,
503
- name="dataVisualization",
504
- args_schema=QueryInput,
505
- output_schema=QueryOutput,
506
- description=""" Tool to generate a visual chart output for a particular warehouse based on the provided question.
507
- This tool processes the user question to identify the warehouse name and the specific request. If the user specifies
508
- an email, include the email in the input. The input format should be: 'warehouse name: user question: email (if any)'.
509
- The tool generates the requested chart and sends it to the provided email if specified.
510
- Examples:
511
- 1. Question without email, without warehouse: "Analyze item name and quantity in a bar chart in warehouse"
512
- Input to tool: "I want to analyze item name and quantity in a bar chart"
513
- 2. Question with email: "Analyze item name and quantity in a bar chart in warehouse Allcargo Logistics and send email to [email protected]"
514
- Input to tool: "Allcargo Logistics: I want to analyze item name and quantity in a bar chart: [email protected]"
515
- """
516
- )
517
- ]
518
-
519
- prompt_template = f"""You are an assistant that helps with database queries, API information, and document retrieval. Your job is to provide clear, complete, and detailed responses to the following queries. Please give the output response in an user friendly way and remove "**" from the response. For example, document related queries can be answered in a clear and concise way with numbering and not as a paragraph. Database related queries should be answered with proper indentation and use numbering for the rows. ASN id related queries should be answered with proper indentation and use numbering for the rows.
520
-
521
- For ASN id related questions, if the user specifies an ASN id, provide the information from the api tool. Pass only the id as input to the tool. Do not pass the entire question as input to the tool. If the details are not found, say it in a clear and concise way.
522
- You are an AI assistant trained to help with warehouse management questions based on a detailed document about our WMS. The document covers various processes such as ASN handling, purchase orders, cross docking, appointment scheduling for shipments, and yard management. Please provide a complete and concise response within 200 words and Ensure that the response is not truncated and covers the essential points. When answering, focus on providing actionable insights and clear explanations related to the specific query. Please remove "**" from the response.
523
- For SQL database-related questions, only use the fields available in the warehouse schema, including tables such as customer_master, efs_company_master, efs_group_company_master, efs_region_master, party_address_detail, wms_warehouse_master.
524
- For datavisualization, user will ask for inventory report of a particular warehouse. Your job is to return the image path to chat interface and display the image as output.
525
- {{agent_scratchpad}}
526
- Here is the information you need to process:
527
- Question: {{input}}"""
528
- agent_executor = bind_llm(llm,tools,prompt_template)
529
-
530
- def ensure_temp_chart_dir():
531
- temp_chart_dir = os.getenv("IMAGE_MAIN_URL")
532
- if not os.path.exists(temp_chart_dir):
533
- os.makedirs(temp_chart_dir)
534
-
535
- def clean_gradio_tmp_dir():
536
- tmp_dir = os.getenv("IMAGE_GRADIO_PATH")
537
- if os.path.exists(tmp_dir):
538
- try:
539
- shutil.rmtree(tmp_dir)
540
- except Exception as e:
541
- print(f"Error cleaning up /tmp/gradio/ directory: {e}")
542
-
543
-
544
- # Define the interface function
545
- max_iterations = 5
546
- iterations = 0
547
-
548
-
549
- def handle_query(user_question, chatbot, audio=None):
550
-
551
- """
552
- Function to handle the processing of user input with `AgentExecutor.invoke()`.
553
- """
554
- global current_event, stop_event
555
-
556
- # Clear previous stop event and current_event
557
- stop_event.clear()
558
-
559
- if current_event and not current_event.done():
560
- chatbot.append(("","A query is already being processed. Please stop it before starting a new one."))
561
- return gr.update(value=chatbot)
562
-
563
- # Start the processing in a new thread
564
- current_event = executor.submit(answer_question_thread, user_question, chatbot)
565
-
566
- # Periodically check if current_event is done
567
- while not current_event.done():
568
- if stop_event.is_set():
569
- #current_event.task.cancel() # Attempt to cancel the current_event
570
- current_event.set_result((user_question, "Sorry, we encountered an error while processing your request. Please try after some time."))
571
- current_event.cancel() # Attempt to cancel the current_event
572
- executor.shutdown(wait=False) # Shutdown the executor
573
- print("Current event cancelled")
574
- print(current_event.cancelled())
575
-
576
- chatbot.append((user_question, "Sorry, we encountered an error while processing your request. Please try after some time."))
577
- return gr.update(value=chatbot)
578
-
579
- time.sleep(1) # Wait for 1 second before checking again
580
-
581
- if current_event.cancelled():
582
- chatbot.append((user_question, "Sorry, we encountered an error while processing your request. Please try after some time."))
583
- return gr.update(value=chatbot)
584
- else:
585
- try:
586
- user_question1, response_text1 = current_event.result() # Get the result of the completed current_event
587
- print("output")
588
- print(user_question1)
589
- print(response_text1)
590
- chatbot.append((user_question1, response_text1))
591
- return gr.update(value=chatbot)
592
- except Exception as e:
593
- print(f"Error occurred: {e}")
594
- chatbot.append((user_question, "Sorry, we encountered an error while processing your request. Please try after some time."))
595
- return gr.update(value=chatbot)
596
-
597
-
598
- def stop_processing(chatbot):
599
- """
600
- Stops the current processing if it's running.
601
- """
602
- global current_event, stop_event
603
- if current_event and not current_event.done():
604
- stop_event.set() # Signal the process to stop
605
- current_event.cancel() # Attempt to cancel the current_event
606
- chatbot.append(("Sorry, we encountered an error while processing your request. Please try after some time.",""))
607
- return gr.update(value=chatbot)
608
-
609
- # This function is for agent executor invoke with the option of stop
610
- def answer_question_thread(user_question, chatbot,audio=None):
611
-
612
- global iterations
613
- iterations = 0
614
- # Ensure the temporary chart directory exists
615
- # ensure_temp_chart_dir()
616
- # Clean the /tmp/gradio/ directory
617
- # clean_gradio_tmp_dir()
618
- # Handle audio input if provided
619
- """
620
- if audio is not None:
621
- sample_rate, audio_data = audio
622
- audio_segment = AudioSegment(
623
- audio_data.tobytes(),
624
- frame_rate=sample_rate,
625
- sample_width=audio_data.dtype.itemsize,
626
- channels=1
627
- )
628
- with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_audio_file:
629
- audio_segment.export(temp_audio_file.name, format="wav")
630
- temp_audio_file_path = temp_audio_file.name
631
-
632
- recognizer = sr.Recognizer()
633
- with sr.AudioFile(temp_audio_file_path) as source:
634
- audio_content = recognizer.record(source)
635
- try:
636
- user_question = recognizer.recognize_google(audio_content)
637
- except sr.UnknownValueError:
638
- user_question = "Sorry, I could not understand the audio."
639
- except sr.RequestError:
640
- user_question = "Could not request results from Google Speech Recognition service."
641
- """
642
-
643
- while iterations < max_iterations:
644
-
645
- response = agent_executor.invoke({"input": user_question}, config={"callbacks": [langfuse_handler]}, early_stopping_method="generate")
646
-
647
- if isinstance(response, dict):
648
- response_text = response.get("output", "")
649
- else:
650
- response_text = response
651
- if "invalid" not in response_text.lower():
652
- break
653
- iterations += 1
654
-
655
- if iterations == max_iterations:
656
- return user_question , "Sorry, I couldn't complete your request" #"The agent could not generate a valid response within the iteration limit."
657
-
658
- if os.getenv("IMAGE_PATH") in response_text:
659
- # Open the image file
660
- img = Image.open(os.getenv("IMAGE_PATH"))
661
-
662
- # Convert the PIL Image to a base64 encoded string
663
- buffered = BytesIO()
664
- img.save(buffered, format="PNG")
665
- img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
666
-
667
- img = f'<img src="data:image/png;base64,{img_str}" style="width:450px; height:400px;">'
668
-
669
- response_text = response.get("output", "").split(".")[0] + img
670
-
671
- email_pattern = r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}'
672
- match = re.search(email_pattern, user_question)
673
- if match:
674
- user_email = match.group() # Return the matched email
675
-
676
- # email send
677
- if len(user_email) > 0:
678
- # Send email with the chart image attached
679
- send_email_with_attachment_mailjet(
680
- recipient_email=user_email,
681
- subject="Warehouse Inventory Report",
682
- body=response.get("output", "").split(".")[0] + ". This is an auto-generated email containing a chart created using Generative AI.",
683
- # attachment_path=chart_path
684
- attach_img_base64=img_str)
685
-
686
-
687
- if "send email to" in user_question:
688
- try:
689
- os.remove(img) # Clean up the temporary image file
690
- except Exception as e:
691
- print(f"Error cleaning up image file: {e}")
692
- except Exception as e:
693
- print(f"Error loading image file: {e}")
694
- response_text = "Chart generation failed. Please try again."
695
-
696
- return user_question, response_text
697
- else:
698
- return user_question, response_text
699
- # response_text = response_text.replace('\n', ' ').replace(' ', ' ').strip()
700
- # return response_text
701
-
702
-
703
- # without forceful stop option
704
- def answer_question(user_question, chatbot, audio=None):
705
-
706
- global iterations
707
- iterations = 0
708
- # Ensure the temporary chart directory exists
709
- # ensure_temp_chart_dir()
710
- # Clean the /tmp/gradio/ directory
711
- # clean_gradio_tmp_dir()
712
- # Handle audio input if provided
713
- if audio is not None:
714
- sample_rate, audio_data = audio
715
- audio_segment = AudioSegment(
716
- audio_data.tobytes(),
717
- frame_rate=sample_rate,
718
- sample_width=audio_data.dtype.itemsize,
719
- channels=1
720
- )
721
- with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_audio_file:
722
- audio_segment.export(temp_audio_file.name, format="wav")
723
- temp_audio_file_path = temp_audio_file.name
724
-
725
- recognizer = sr.Recognizer()
726
- with sr.AudioFile(temp_audio_file_path) as source:
727
- audio_content = recognizer.record(source)
728
- try:
729
- user_question = recognizer.recognize_google(audio_content)
730
- except sr.UnknownValueError:
731
- user_question = "Sorry, I could not understand the audio."
732
- except sr.RequestError:
733
- user_question = "Could not request results from Google Speech Recognition service."
734
-
735
- while iterations < max_iterations:
736
-
737
- response = agent_executor.invoke({"input": user_question}, config={"callbacks": [langfuse_handler]})
738
-
739
- if isinstance(response, dict):
740
- response_text = response.get("output", "")
741
- else:
742
- response_text = response
743
- if "invalid" not in response_text.lower():
744
- break
745
- iterations += 1
746
-
747
- if iterations == max_iterations:
748
- return "The agent could not generate a valid response within the iteration limit."
749
-
750
-
751
-
752
- if os.getenv("IMAGE_PATH") in response_text:
753
- # Open the image file
754
- img = Image.open(os.getenv("IMAGE_PATH"))
755
-
756
- # Convert the PIL Image to a base64 encoded string
757
- buffered = BytesIO()
758
- img.save(buffered, format="PNG")
759
- img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
760
-
761
- img = f'<img src="data:image/png;base64,{img_str}" style="width:450px; height:400px;">'
762
-
763
- chatbot.append((user_question, img))
764
-
765
- email_pattern = r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}'
766
- match = re.search(email_pattern, user_question)
767
- if match:
768
- user_email = match.group() # Return the matched email
769
-
770
- # email send
771
- if len(user_email) > 0:
772
- # Send email with the chart image attached
773
- send_email_with_attachment_mailjet(
774
- recipient_email=user_email,
775
- subject="Warehouse Inventory Report",
776
- body=response.get("output", "").split(".")[0],
777
- # attachment_path=chart_path
778
- attachment_path=img_str)
779
-
780
- # Send email with the chart image attached
781
- """send_email_with_attachment(
782
- recipient_email=user_email,
783
- subject="Warehouse Inventory Report",
784
- body=response.get("output", "").split(":")[0],
785
- # attachment_path=chart_path
786
- attachment_path=os.getenv("IMAGE_PATH")
787
- )"""
788
-
789
- if "send email to" in user_question:
790
- try:
791
- os.remove(img) # Clean up the temporary image file
792
- except Exception as e:
793
- print(f"Error cleaning up image file: {e}")
794
- except Exception as e:
795
- print(f"Error loading image file: {e}")
796
- chatbot.append((user_question, "Chart generation failed. Please try again."))
797
- return gr.update(value=chatbot)
798
-
799
- else:
800
- chatbot.append((user_question, response_text))
801
- return gr.update(value=chatbot)
802
-
803
-
804
- def submit_feedback(feedback, chatbot, request: gr.Request):
805
- gr.Info("Thank you for your feedback.")
806
- #save feedback with user question and response in database
807
- save_feedback(request.username,chatbot[-1][0], chatbot[-1][1], feedback)
808
- feedback_response = "User feedback: " + feedback
809
- return chatbot + [(feedback_response, None)], gr.update(visible=False), gr.update(visible=False)
810
-
811
-
812
- # Function to connect to MySQL database
813
- def connect_to_db():
814
- return mysql.connector.connect(
815
- host=DB_HOST,
816
- user=DB_USER,
817
- password=DB_PASSWORD,
818
- database=DB_NAME
819
- )
820
-
821
- # Function to save feedback to the database
822
- def save_feedback(username, user_question, user_response, feedback):
823
- try:
824
- conn = connect_to_db()
825
- cursor = conn.cursor()
826
- query = "INSERT INTO user_feedback (username, question, response, feedback) VALUES (%s, %s, %s, %s)"
827
- cursor.execute(query, (username, user_question, user_response, feedback))
828
- conn.commit()
829
- except mysql.connector.Error as err:
830
- print(f"Error: {err}")
831
- finally:
832
- if cursor:
833
- cursor.close()
834
- if conn:
835
- conn.close()
836
-
837
- def handle_dislike(data: gr.LikeData):
838
- if not data.liked:
839
- print("downvote")
840
- gr.Info("Please enter your feedback.")
841
- return gr.update(visible=True), gr.update(visible=True)
842
- else:
843
- print("upvote")
844
- return gr.update(visible=False), gr.update(visible=False)
845
-
846
- # greet with user name on successful login
847
- def update_message(request: gr.Request):
848
- return f"<h2 style=' font-family: Calibri;'>Welcome, {request.username}</h4>"
849
-
850
- # Function to generate a 50-word summary of the newly uploaded doc using OpenAI
851
- def generate_summary(text):
852
- prompt = (
853
- "You are an AI that helps with document analysis. Please provide a concise title and a summary of the following document. "
854
- "The summary should be about 50 words and include key details that can help answer questions accurately:\n\n"
855
- f"{text}\n\nTitle : Summary"
856
- )
857
- # Call the OpenAI API to generate a summary
858
- response = openai.chat.completions.create(
859
- messages=[
860
- {
861
- "role": "user",
862
- "content": prompt,
863
- }
864
- ],
865
- model="gpt-4o-mini",
866
- )
867
- # Extract the title and summary from the response
868
- response_content = response.choices[0].message.content
869
- lines = response_content.split("\n")
870
- # Extract title
871
- title_line = lines[0]
872
- title = title_line.split("**Title:**")[-1].strip()
873
-
874
- # Extract summary
875
- summary_line = lines[2]
876
- summary = summary_line.split("**Summary:**")[-1].strip()
877
-
878
- return title, summary
879
- #function to handle file upload decide whether excel or doc is uploaded and respective tool will be created with appropriate prompts at runtime
880
- def upload_file(filepath):
881
- global vector_store1, file_extension
882
-
883
- # Get the file extension
884
- _, file_extension = os.path.splitext(filepath)
885
-
886
- if file_extension == ".pdf":
887
- texts1 = load_and_split_pdf(filepath)
888
-
889
- vector_store1 = create_vector_store(texts1)
890
- # Generate a 50-word summary from the extracted text
891
- title, summary = generate_summary(texts1)
892
- #return title, summary, file_extension
893
- success_msg = add_to_redmindgpt(title, summary)
894
- elif file_extension == ".xlsx":
895
- title, prompt = process_excel(filepath)
896
- #return title, prompt
897
- success_msg = add_to_redmindgpt(title, prompt)
898
- return success_msg
899
-
900
- def generate_example_questions(sheet_name, column_headers):
901
- """
902
- Generates natural language questions based on column headers.
903
-
904
- Args:
905
- sheet_name (str): The name of the Excel sheet.
906
- column_headers (list): List of column headers from the sheet.
907
-
908
- Returns:
909
- questions (list): List of generated questions based on the columns.
910
- """
911
- questions = []
912
-
913
- # Check for typical columns and create questions
914
- if 'Product Name' in column_headers or 'Product' in column_headers:
915
- questions.append(f"What is the total sales for a specific product in {sheet_name}?")
916
-
917
- if 'Sales Amount' in column_headers or 'Amount' in column_headers:
918
- questions.append(f"What is the total sales amount for a specific region in {sheet_name}?")
919
-
920
- if 'Region' in column_headers:
921
- questions.append(f"Which region had the highest sales in {sheet_name}?")
922
-
923
- if 'Date' in column_headers:
924
- questions.append(f"What were the total sales during a specific month in {sheet_name}?")
925
-
926
- if 'Price' in column_headers:
927
- questions.append(f"What is the price of a specific product in {sheet_name}?")
928
-
929
- if any(fnmatch.fnmatch(header, 'Employee*') for header in column_headers):
930
- questions.append(f"What are the details of the distinct broker names?")
931
-
932
- return questions
933
-
934
- def generate_prompt_from_excel_file(df_dict):
935
- """
936
- Generates a prompt from an Excel file containing multiple sheets.
937
-
938
- Args:
939
- excel_file_path (str): The path to the Excel file.
940
-
941
- Returns:
942
- prompt (str): A detailed prompt including sheet names, column headers, sample data,
943
- and example questions for each sheet.
944
- """
945
-
946
- # Initialize prompt with basic structure
947
- prompt = "You have been provided with an Excel file containing data in several sheets.\n"
948
-
949
- # Loop through each sheet to extract column headers and sample data
950
- for sheet_name, sheet_df in df_dict.items():
951
- # Extract column headers
952
- column_headers = list(sheet_df.columns)
953
-
954
- # Get a sample of the data (first few rows)
955
- sample_data = sheet_df.head(3).to_string(index=False)
956
-
957
- # Add sheet details to the prompt
958
- prompt += f"For the sheet '{sheet_name}', the column headers are:"
959
- prompt += f"{', '.join(column_headers)}\n\n"
960
- #prompt += f"Example data from sheet '{sheet_name}':\n"
961
- #prompt += f"{sample_data}\n\n"
962
-
963
- # Generate example natural language questions based on columns
964
- example_questions = generate_example_questions(sheet_name, column_headers)
965
- #prompt += "### Example Questions:\n"
966
- #for question in example_questions:
967
- # prompt += f"- {question}\n"
968
- #prompt += "\n"
969
-
970
- # Finalize the prompt with function call description
971
-
972
- prompt += f"- Query: A natural language question (e.g., List all the employees with broker name ADP or Alerus). The question should be sent as 'What are the employee details with broker name ADP or Alerus :'."
973
- prompt += f"""Output : {docstatus}. Here is the sample table:
974
- {sample_table}.
975
- """
976
-
977
- prompt += f"- Query: A natural language question with request to create LOA document (e.g., can you create LOA document for all the employees with broker name ADP or Alerus). The question should be sent as 'What are the employee details with broker name ADP or Alerus : LOA document'."
978
- prompt += f"""Output: {docstatus}. Here is the sample table:
979
- {sample_table}.
980
- If there is any error, please display the message returned by the function as response. """
981
-
982
-
983
- return "Excel data", prompt
984
-
985
- # Function to handle "Add to RedMindGPT" button click
986
- def add_to_redmindgpt(title, summary):
987
- """
988
- Adds a document or Excel file to the RedmindGPT system and configures the appropriate runtime tool for handling related queries.
989
- Parameters:
990
- title (str): The title of the document or Excel file.
991
- summary (str): A brief summary of the document or Excel file.
992
- Returns:
993
- str: A message indicating whether the file has been added successfully.
994
- Behavior:
995
- - If the file extension is ".pdf", it sets up a runtime tool for handling document-related queries.
996
- - If the file extension is ".xlsx", it sets up a runtime tool for handling Excel data-related queries.
997
- - Configures the prompt template for the agent executor based on the file type.
998
- - Adds the configured runtime tool to the list of tools used by the agent executor.
999
- """
1000
-
1001
- global agent_executor, file_extension
1002
-
1003
- if file_extension == ".pdf":
1004
- run_time_tool_summary = f"For {title} document related questions, Please refer runtimeDocumentData tool. {summary}. Please provide a complete and concise response within 200 words and Ensure that the response is not truncated and covers the essential points."
1005
-
1006
- run_time_tool = StructuredTool(
1007
- func=document_data_tool_runtime,
1008
- name="runtimeDocumentData",
1009
- args_schema=QueryInput,
1010
- output_schema=QueryOutput,
1011
- description=f"You are an AI assistant trained to help with the questions based on the uploaded document {title}. {summary}. Please provide a complete and concise response within 200 words and Ensure that the response is not truncated and covers the essential points."
1012
- )
1013
-
1014
- # Add the new tool to the beginning
1015
- tools.insert(0, run_time_tool)
1016
-
1017
- prompt_template = f"""You are an assistant that helps with database queries, API information, and document retrieval. Your job is to provide clear, complete, and detailed responses to the following queries. Please give the output response in an user friendly way and remove "**" from the response. For example, document related queries can be answered in a clear and concise way with numbering and not as a paragraph. Database related queries should be answered with proper indentation and use numbering for the rows. ASN id related queries should be answered with proper indentation and use numbering for the rows.
1018
- {run_time_tool_summary}
1019
- For ASN id related questions, if the user specifies an ASN id, provide the information from the api tool. Pass only the id as input to the tool. Do not pass the entire question as input to the tool. If the details are not found, say it in a clear and concise way.
1020
- You are an AI assistant trained to help with warehouse management questions based on a detailed document about our WMS. The document covers various processes such as ASN handling, purchase orders, cross docking, appointment scheduling for shipments, and yard management. Please provide a complete and concise response within 200 words and Ensure that the response is not truncated and covers the essential points. When answering, focus on providing actionable insights and clear explanations related to the specific query. Please remove "**" from the response.
1021
- For SQL database-related questions, only use the fields available in the warehouse schema, including tables such as customer_master, efs_company_master, efs_group_company_master, efs_region_master, party_address_detail, wms_warehouse_master.
1022
- For datavisualization, user will ask for inventory report of a particular warehouse. Your job is to return the image path to chat interface and display the image as output.
1023
-
1024
- {{agent_scratchpad}}
1025
- Here is the information you need to process:
1026
- Question: {{input}}"""
1027
- agent_executor = bind_llm(llm,tools,prompt_template)
1028
- return f"File has been added successfully."
1029
- elif file_extension == ".xlsx":
1030
- run_time_excel_tool_summary = f"For {title} related questions, Please refer runtimeExcelData tool. {summary}. Display the response only in the format as mentioned in the tool description. "
1031
-
1032
- run_time_excel_tool = StructuredTool(
1033
- func=chat_with_excel_data_dataframe,
1034
- name="runtimeExcelData",
1035
- args_schema=QueryInput,
1036
- output_schema=QueryOutput,
1037
- description=f"""You are an AI assistant trained to handle Excel data and return meaningful insights. If user query is given with an option of generating the document with the result set dataframe, pass two inputs to the tool. First input is the user query and the second input will be the phrase "create document". display the response only in the below format.
1038
- {docstatus}. Here is the sample data:
1039
- {sample_table}.
1040
- Please provide the total rows count from the {total_rows} values returned by the function and not the count of sample table rows. If there is any error, please display the message returned by the function as response. """
1041
- )
1042
-
1043
- # Add the new tool to the beginning
1044
- tools.insert(0, run_time_excel_tool)
1045
-
1046
- prompt_template = f"""You are an assistant that helps with database queries, API information, and document retrieval. Your job is to provide clear, complete, and detailed responses to the following queries. Please give the output response in an user friendly way and remove "**" from the response. For example, document related queries can be answered in a clear and concise way with numbering and not as a paragraph. Database related queries should be answered with proper indentation and use numbering for the rows. ASN id related queries should be answered with proper indentation and use numbering for the rows.
1047
- {run_time_excel_tool_summary}
1048
- For ASN id related questions, if the user specifies an ASN id, provide the information from the api tool. Pass only the id as input to the tool. Do not pass the entire question as input to the tool. If the details are not found, say it in a clear and concise way.
1049
- You are an AI assistant trained to help with warehouse management questions based on a detailed document about our WMS. The document covers various processes such as ASN handling, purchase orders, cross docking, appointment scheduling for shipments, and yard management. Please provide a complete and concise response within 200 words and Ensure that the response is not truncated and covers the essential points. When answering, focus on providing actionable insights and clear explanations related to the specific query. Please remove "**" from the response.
1050
- For SQL database-related questions, only use the fields available in the warehouse schema, including tables such as customer_master, efs_company_master, efs_group_company_master, efs_region_master, party_address_detail, wms_warehouse_master.
1051
- For datavisualization, user will ask for inventory report of a particular warehouse. Your job is to return the image path to chat interface and display the image as output.
1052
-
1053
- {{agent_scratchpad}}
1054
- Here is the information you need to process:
1055
- Question: {{input}}"""
1056
- agent_executor = bind_llm(llm,tools,prompt_template)
1057
- return f"File has been added successfully."
1058
-
1059
- def process_excel(file):
1060
- global excel_dataframe
1061
- # Check if the file is None
1062
- if file is None:
1063
- return "Excel file", "Your excel does not have values. Please upload a different file." # Return an empty dataframe if no file is uploaded
1064
- else:
1065
- # Read the uploaded Excel file
1066
- excel_dataframe = pd.read_excel(file.name, sheet_name=None) # 'file.name' to get the actual file path
1067
-
1068
- #to get title and summary of excel file
1069
- title, prompt = generate_prompt_from_excel_file(excel_dataframe)
1070
- excel_dataframe = pd.read_excel(file.name)
1071
-
1072
- return title, prompt # Return the success message.
1073
-
1074
- def chat_with_excel_data(question):
1075
- global excel_dataframe
1076
- response_dataframe = chat_with_llm(excel_dataframe,question)
1077
- print(response_dataframe)
1078
- return response_dataframe
1079
-
1080
- def chat_with_excel_data_dataframe(question):
1081
- isDataFrame = True
1082
- print(f"question for excel data frame : {question}")
1083
- if "LOA" in question:
1084
- #question = question.replace("create document", "").strip()
1085
- create_document = True
1086
- else:
1087
- create_document = False
1088
- print(f"create document : {create_document}")
1089
- response_dataframe = chat_with_excel_data(question)
1090
- if isinstance(response_dataframe, pd.DataFrame) == False:
1091
-
1092
- print("The result is not a DataFrame.")
1093
- if ":" in response_dataframe:
1094
- isDataFrame = False
1095
- names_part = response_dataframe.split(":", 1)[1] # Get everything after the colon and space
1096
-
1097
- # Split the names by commas to create a list
1098
- names = names_part.split(",")
1099
-
1100
- # Convert the list of names to a DataFrame
1101
- response_dataframe = pd.DataFrame(names, columns=["Result"])
1102
-
1103
-
1104
- #handle large dataset
1105
- response = handle_large_dataset(response_dataframe, create_document,isDataFrame)
1106
-
1107
- return response
1108
-
1109
- #Save the respnse dataframe to an Excel file in hostinger so that the user can download it
1110
- #save_file_path = "dataframe_output.xlsx"
1111
- #response_dataframe.to_excel(save_file_path, index=False)
1112
- #save_file_to_hostinger(save_file_path)
1113
-
1114
- # Check if the response is a DataFrame
1115
- """if isinstance(response_dataframe, pd.DataFrame):
1116
- # Convert DataFrame to HTML for display
1117
- df_html = response_dataframe.to_html(classes='dataframe', index=False)
1118
- print(f"dfhtml:{df_html}")
1119
- return df_html"""
1120
-
1121
- #return response_dataframe.head(10)#, len(response_dataframe)
1122
-
1123
- def save_file_to_hostinger(save_file_path):
1124
- from ftplib import FTP
1125
- # Step 2: FTP server credentials
1126
- ftp_host = 'ftp.redmindtechnologies.com' # Replace with your FTP server address
1127
- ftp_user = 'u852023448.redmindGpt' # Replace with your FTP username
1128
- ftp_pass = 'RedMind@505' # Replace with your FTP password
1129
- remote_file_path = '/RedMindGPT/output.xlsx' # Replace with the desired path on the server
1130
-
1131
- # Create an FTP connection
1132
- ftp = FTP(ftp_host)
1133
- ftp.login(ftp_user, ftp_pass)
1134
-
1135
- # Open the local file and upload it to the server
1136
- with open(save_file_path, 'rb') as file:
1137
- ftp.storbinary(f'STOR {remote_file_path}', file)
1138
-
1139
- print(f'File {save_file_path} uploaded to {remote_file_path} on server.')
1140
-
1141
- # Close the FTP connection
1142
- ftp.quit()
1143
-
1144
- def handle_large_dataset(df, create_document,isDataFrame):
1145
-
1146
- total_rows = len(df)
1147
- #print(df)
1148
- print(f"Total rows: {total_rows}")
1149
-
1150
- #docstatus = f"Download the complete dataset <a href='https://huggingface.co/spaces/Redmind/NewageNXTGPT/blob/main/assets/output.xlsx' download> here.</a>.There are total of {total_rows} rows."
1151
- docstatus = f"Download the complete dataset <a href='https://redmindtechnologies.com/RedMindGPT/output.xlsx' download> here.</a>.There are total of {total_rows} rows."
1152
- if total_rows < 4000:
1153
-
1154
- # 1. Limit to first 10 rows
1155
-
1156
-
1157
- # 2. Handle missing values
1158
- #limited_data.fillna("N/A", inplace=True)
1159
- # 3. Drop the original first column
1160
- if len(df.columns) > 1:
1161
- # Skipping the original first column
1162
- limited_data = df.head(3)
1163
- limited_data_without_first_column = limited_data.iloc[:, 1:]
1164
- else:
1165
- limited_data = df.head(20)
1166
- limited_data_without_first_column = limited_data
1167
- #print( "range "+ len(limited_data_without_first_column))
1168
- # 4. Add SNo (serial number) as the first column, starting from 1
1169
- if isDataFrame :
1170
-
1171
- limited_data_without_first_column.insert(0, 'SNo', range(1, len(limited_data_without_first_column) + 1))
1172
- else:
1173
-
1174
- limited_data_without_first_column.insert(0, 'SNo', range(1, len(limited_data) + 1))
1175
- # 3. Save the full dataset to a downloadable file
1176
-
1177
-
1178
- import os
1179
-
1180
- # Get the current working directory
1181
- current_folder = os.getcwd()
1182
-
1183
-
1184
-
1185
- file_path = os.path.join(current_folder, 'output_data.xlsx')
1186
- df.to_excel(file_path, index=False)
1187
- #if not os.path.exists("/data/"):
1188
- #os.makedirs("/data/")
1189
- #df.to_excel('/data/output_data.xlsx', index=False)
1190
-
1191
- files = os.listdir(current_folder)
1192
- print(f"Files in persistent storage: {files}")
1193
- print(f"The current folder is: {current_folder}")
1194
- """from huggingface_hub import Repository
1195
-
1196
- repo = Repository(
1197
- local_dir="./",
1198
- repo_type="space",
1199
- repo_id="Redmind/NewageNXTGPT",
1200
- use_auth_token=os.getenv("HF_TOKEN"),
1201
- )"""
1202
-
1203
- file_path = "output_data.xlsx"
1204
- #download_url = repo.get_download_url(file_path)
1205
-
1206
- from huggingface_hub import upload_file
1207
-
1208
- # Upload file to the Hugging Face Hub
1209
- repo_id = "Redmind/NewageNXTGPT"
1210
- #file_path = "/app/example.txt" # Path to the file to upload
1211
- from huggingface_hub import login
1212
-
1213
- # Login to Hugging Face Hub
1214
- login(token=os.getenv("HF_TOKEN"))
1215
- from huggingface_hub import HfApi
1216
- api = HfApi()
1217
- #api.upload_file(path_or_fileobj=file_path, repo_id=repo_id, repo_type= "space", path_in_repo="static/output.xlsx")
1218
-
1219
- from huggingface_hub import hf_hub_url
1220
-
1221
- print(hf_hub_url(
1222
- repo_id="Redmind/NewageNXTGPT", filename="static/output.xlsx"
1223
- ))
1224
-
1225
- #print(f"Download the file here: {download_url}")
1226
- #save_file_to_hostinger('output_data.xlsx')
1227
- # 4. Create a summary and table of the first 10 rows for display
1228
-
1229
- #columns = list(df.columns)
1230
- sample_table = limited_data_without_first_column.to_markdown()
1231
- #print(sample_table)
1232
- if create_document:
1233
- #Logic to generate pdfs with employee name and account number
1234
- for index, row in df.iterrows():
1235
- # Create a PDF for each row
1236
- create_pdf(row['COMPANY'], row['EMPLOYEE NAME'], row['ACCOUNT NUMBER'])
1237
- create_document = False
1238
- docstatus = f"Please download the complete dataset here: <a href='https://redmindtechnologies.com/RedMindGPT/output.zip' download>Download</a>. {total_rows} documents are created successfully."
1239
- print(sample_table)
1240
- # 5. Return the summary and downloadable link
1241
- #return f"""
1242
- #There are a total of {total_rows} rows. Please download the complete dataset here: <a href="https://redmindtechnologies.com/RedMindGPT/output.xlsx" download>Download</a>. Here are the first 3 rows:
1243
- #{sample_table} """
1244
-
1245
- return sample_table, docstatus
1246
-
1247
- else:
1248
- return "Your query returns a large dataset which is not supported in the current version. Please try a different query."
1249
-
1250
- def create_pdf(cname,ename,account_number):
1251
-
1252
- filled = FormWrapper("LOA_Sample_new.pdf").fill(
1253
- {
1254
- 'company name': cname,
1255
- 'employee name': ename,
1256
- 'account number': account_number
1257
-
1258
-
1259
- },
1260
- )
1261
- output_file_name = f"documents\\{ename}_{cname}.pdf"
1262
- with open(output_file_name, "wb+") as output:
1263
- output.write(filled.read())
1264
-
1265
- repo_id = "Redmind/NewageNXTGPT"
1266
- file_output=f"static/{output_file_name}"
1267
- from huggingface_hub import HfApi
1268
- api = HfApi()
1269
- #api.upload_file(path_or_fileobj=output_file_name, repo_id=repo_id, repo_type= "space", path_in_repo=file_output)
1270
- return f"{output_file_name} is created successfully."
1271
-
1272
-
1273
- css = """
1274
-
1275
- /* Example of custom button styling */
1276
- .gr-button {
1277
- background-color: #6366f1; /* Change to your desired button color */
1278
- color: white;
1279
- border-radius: 8px; /* Make the corners rounded */
1280
- border: none;
1281
- padding: 10px 20px;
1282
- font-size: 12px;
1283
- cursor: pointer;
1284
- }
1285
-
1286
- .gr-button:hover {
1287
- background-color: #8a92f7; /* Darker shade on hover */
1288
- }
1289
-
1290
- .gr-buttonbig {
1291
- background-color: #6366f1; /* Change to your desired button color */
1292
- color: white;
1293
- border-radius: 8px; /* Make the corners rounded */
1294
- border: none;
1295
- padding: 10px 20px;
1296
- font-size: 14px;
1297
- cursor: pointer;
1298
- }
1299
-
1300
- .gr-buttonbig:hover {
1301
- background-color: #8a92f7; /* Darker shade on hover */
1302
- }
1303
-
1304
- /* Customizing the Logout link to be on the right */
1305
- .logout-link {
1306
- text-align: right;
1307
- display: inline-block;
1308
- width: 100%;
1309
- }
1310
-
1311
- .logout-link a {
1312
- color: #4A90E2; /* Link color */
1313
- text-decoration: none;
1314
- font-size: 16px;
1315
- }
1316
-
1317
- .chatbot_gpt {
1318
- height: 600px !important; /* Adjust height as needed */
1319
- }
1320
-
1321
- .logout-link a:hover {
1322
- text-decoration: underline; /* Underline on hover */
1323
- }
1324
-
1325
- .message-buttons-right{
1326
- display: none !important;
1327
- }
1328
-
1329
- body, .gradio-container {
1330
- margin: 0;
1331
- padding: 0;
1332
- }
1333
-
1334
- /* Styling the tab header with a blue background */
1335
- .gr-tab-header {
1336
- background-color: #4A90E2; /* Blue background for the tab header */
1337
- padding: 10px;
1338
- border-radius: 8px;
1339
- color: white;
1340
- font-size: 16px;
1341
- }
1342
-
1343
- /* Styling the selected tab text color to be green */
1344
- .gr-tab-header .gr-tab-active {
1345
- color: green; /* Change selected tab text to green */
1346
- }
1347
-
1348
- /* Keep non-selected tab text color white */
1349
- .gr-tab-header .gr-tab {
1350
- color: white;
1351
- }
1352
-
1353
- /* Custom CSS for reducing the size of the video element */
1354
- .video-player {
1355
- width: 500px; /* Set a custom width for the video */
1356
- height: 350px; /* Set a custom height for the video */
1357
- margin: 0 auto; /* Center the video horizontally */
1358
- }
1359
- """
1360
- with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
1361
- gr.HTML("<CENTER><B><h1 style='font-size:30px; font-family: Calibri;'>RedMindGPT</h1></B></CENTER>")
1362
- # Logout link styled as text link in the right corner
1363
- gr.Markdown("<div class='logout-link'><a href='/logout'><b>Logout</b></a></div>")
1364
-
1365
- # Unified RedMindGPT Interface
1366
- with gr.Row():
1367
- m = gr.Markdown()
1368
- demo.load(update_message, None, m)
1369
-
1370
- # Buttons for sample queries
1371
- with gr.Row():
1372
- sample_button = gr.Button("What are the details of ASN24091600002", elem_classes="gr-buttonbig")
1373
- sample_button1 = gr.Button("What are the active warehouses available", elem_classes="gr-buttonbig")
1374
- sample_button2 = gr.Button("Explain Pre-Receiving Yard Management", elem_classes="gr-buttonbig")
1375
- sample_button3 = gr.Button("can you generate a doughnut chart with item name and quantities for warehouse WH1000001", elem_classes="gr-buttonbig")
1376
- sample_button4 = gr.Button("Analyze item name & quantity for different customers in a stacked bar chart for the warehouse WH1000001 & send email to [email protected]", elem_classes="gr-button")
1377
-
1378
- # Chatbot component
1379
- with gr.Row():
1380
- chatbot = gr.Chatbot(label="Select any of the questions listed above to experience RedMindGPT in action.", elem_classes="chatbot_gpt")
1381
-
1382
- # Textbox for user questions
1383
- with gr.Row():
1384
- with gr.Column(scale=1):
1385
- message = gr.Textbox(show_label=False, container=False, placeholder="Please enter your question")
1386
-
1387
- with gr.Row():
1388
- feedback_textbox = gr.Textbox(visible=False, show_label=False, container=False, placeholder="Please enter your feedback.")
1389
- submit_feedback_button = gr.Button("Submit Feedback", visible=False, elem_classes="gr-buttonbig")
1390
- with gr.Column(scale=1):
1391
- with gr.Row():
1392
- button = gr.Button("Submit", elem_id="submit", elem_classes="gr-buttonbig")
1393
- stop_button = gr.Button("Stop", elem_classes="gr-buttonbig")
1394
- # Rearranged to place Upload Doc and Upload Excel in the same row
1395
- with gr.Row():
1396
- with gr.Column(scale=1):
1397
- # File Upload Section
1398
- gr.Markdown("**Add a document or Excel for natural language interaction.**")
1399
- with gr.Column(scale=1):
1400
- u = gr.UploadButton("Upload a doc/excel", file_count="single", elem_classes="gr-buttonbig")
1401
- #excel_file = gr.UploadButton("Upload an excel", file_count="single", elem_classes="gr-buttonbig", file_types=[".xlsx", ".xls"])
1402
- with gr.Column(scale=1):
1403
- add_button = gr.Button("Add to RedMindGPT", elem_classes="gr-buttonbig", visible=False)
1404
- with gr.Row():
1405
- title_textbox = gr.Textbox(label="Title", visible=False)
1406
- summary_textarea = gr.Textbox(label="Summary", lines=5, visible=False)
1407
-
1408
-
1409
- output_message = gr.Markdown() # Markdown to display output message
1410
- success_message = gr.Markdown() # Placeholder for messages
1411
-
1412
-
1413
- # Moved function calling lines to the end
1414
- stop_button.click(stop_processing, [chatbot], [chatbot])
1415
-
1416
- button.click(handle_query, [message, chatbot], [chatbot])
1417
- message.submit(handle_query, [message, chatbot], [chatbot])
1418
- message.submit(lambda x: gr.update(value=""), None, [message], queue=False)
1419
- button.click(lambda x: gr.update(value=''), [], [message])
1420
-
1421
- chatbot.like(handle_dislike, None, outputs=[feedback_textbox, submit_feedback_button])
1422
- submit_feedback_button.click(submit_feedback, [feedback_textbox, chatbot], [chatbot, feedback_textbox, submit_feedback_button])
1423
- submit_feedback_button.click(lambda x: gr.update(value=''), [], [feedback_textbox])
1424
-
1425
- sample_button.click(handle_query, [sample_button, chatbot], [chatbot])
1426
- sample_button1.click(handle_query, [sample_button1, chatbot], [chatbot])
1427
- sample_button2.click(handle_query, [sample_button2, chatbot], [chatbot])
1428
- sample_button3.click(handle_query, [sample_button3, chatbot], [chatbot])
1429
- sample_button4.click(handle_query, [sample_button4, chatbot], [chatbot])
1430
-
1431
- """u.upload(upload_file, u, [title_textbox, summary_textarea])
1432
- u.upload(lambda _: (gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)), None, [title_textbox, summary_textarea, add_button])
1433
- add_button.click(add_to_redmindgpt, [title_textbox, summary_textarea], output_message)
1434
- add_button.click(lambda _: (gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)), None, [title_textbox, summary_textarea, add_button])
1435
- """
1436
- u.upload(upload_file, u, output_message)
1437
- u.upload(lambda _: (gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)), None, [title_textbox, summary_textarea, add_button])
1438
-
1439
-
1440
- demo.launch(auth=[("lakshmi", "redmind"), ("admin", "redmind"), ("arun", "redmind"), ("NewageGlobal", "Newage123$")], auth_message="RedMindGPT", inline=False)