Spaces:
Build error
Build error
Update app.py
Browse files8 unmodified lines910111213141516171819142021222324252627282930313233349 unmodified lines4445463031474849505152535455565758596061626364656667684344697071727321 unmodified lines9596977298991001017 unmodified lines1091101118611211311411559 unmodified lines17517617715217817918018111 unmodified lines1931941951701711961971981992001762012022032042 unmodified lines20720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025115 unmodified lines2672682692062072082092102702712722732142742752762772782792802812822832842852862212222232242252872882897 unmodified lines29729829923924024124224330030130230330421 unmodified lines32632732827127227327432927533027727827928028128228328433133233333433533633733833934034134234334434534634734834935035 unmodified lines38638738832838939039139239339439539639739839940040140240340410 unmodified lines41541641741841942042142242342442542642742842943043143243343434943543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147235435535635735835936036136236347347447547647747847948048148248348448511 unmodified lines49749849950050150250350450550650750850951051151251351447 unmodified lines56256356456556656756820 unmodified lines5895905915928 unmodified linesimport base64from io import BytesIOimport osimport reimport tempfileimport waveimport requestsimport gradio as grimport timeimport shutilimport json#import nltkimport nltk#audio packageimport speech_recognition as srfrom pydub import AudioSegmentfrom pydub.playback import play#email libraryimport smtplibfrom email.mime.multipart import MIMEMultipartfrom email.mime.text import MIMETextfrom email.mime.base import MIMEBasefrom email import encoders#langchainfrom langchain_core.prompts import ChatPromptTemplatefrom langchain_core.output_parsers import StrOutputParserfrom langchain_core.runnables import RunnableSequence, RunnableLambda9 unmodified linesfrom nltk.tokenize import sent_tokenizefrom sqlalchemy import create_enginefrom sqlalchemy.sql import textimport jsonimport nltk
#pandasimport pandas as pdfrom pandasai.llm.openai import OpenAIfrom pandasai import SmartDataframe
nltk.download('punkt')
open_api_key_token = os.environ['OPEN_AI_API']
os.environ['OPENAI_API_KEY'] = open_api_key_tokenpdf_path="Inbound.pdf"
db_uri = 'postgresql+psycopg2://postgres:postpass@193.203.162.39:5432/warehouseAi'# Database setup
db = SQLDatabase.from_uri(db_uri)
# LLM setup#llm = ChatOpenAI(model="gpt-3.5-turbo-0125",max_tokens=150,temperature=0.1)llm = ChatOpenAI(model="gpt-4o-mini",max_tokens=250,temperature=0.1)llm = ChatOpenAI(model="gpt-4o-mini",max_tokens=300,temperature=0.1)llm_chart = OpenAI()
def get_schema(_): schema_info = db.get_table_info() # This should be a string of your SQL schema21 unmodified linesdef run_query(query): # Clean the query by removing markdown symbols and trimming whitespace clean_query = query.replace("```sql", "").replace("```", "").strip() #print(f"Executing SQL Query: {clean_query}") print(f"Executing SQL Query: {clean_query}") try: result = db.run(clean_query) return result7 unmodified linesdef database_tool(question): # print(question) sql_query = generate_sql_query(question) ##print(sql_query) print(sql_query) return run_query(sql_query)
def get_ASN_data(question):59 unmodified lines
def query_vector_store(vector_store, query): docs = vector_store.similarity_search(query, k=5) #print(f"Vector store return: {docs}") print(f"Vector store return: {docs}") return docs
def summarize_document(docs):11 unmodified lines summarized_content = doc_content summarized_docs.append(summarized_content) return '\n\n'.join(summarized_docs)pdf_path = "Inbound.pdf"#pdf_path = r"D:\rajesh\python\chat_agent\Inbound.pdf"
texts = load_and_split_pdf(pdf_path)vector_store = create_vector_store(texts)
def document_data_tool(question): #print(f"Document data tool enter: {question}") print(f"Document data tool enter: {question}") # query_string = question['tags'][0] if 'tags' in question and question['tags'] else "" query_response = query_vector_store(vector_store, question) print("query****")2 unmodified lines #print("summary***") #print(summarized_response) return query_response def send_email_with_attachment(recipient_email, subject, body, attachment_path): sender_email = "redmind.uiautomation@gmail.com" sender_password = "jymz apyc raih eubg"
# Create a multipart message msg = MIMEMultipart() msg['From'] = sender_email msg['To'] = recipient_email msg['Subject'] = subject
# Attach the body with the msg instance msg.attach(MIMEText(body, 'plain'))
# Open the file to be sent attachment = open(attachment_path, "rb")
# Instance of MIMEBase and named as p part = MIMEBase('application', 'octet-stream')
# To change the payload into encoded form part.set_payload((attachment).read())
# Encode into base64 encoders.encode_base64(part)
part.add_header('Content-Disposition', f"attachment; filename= {attachment_path}")
# Attach the instance 'part' to instance 'msg' msg.attach(part)
# Create SMTP session for sending the mail server = smtplib.SMTP('smtp.gmail.com', 587) server.starttls() server.login(sender_email, sender_password) text = msg.as_string() server.sendmail(sender_email, recipient_email, text) server.quit() #return 1
def make_api_request(url, params): import requests15 unmodified lines "url": "http://193.203.162.39:9090/nxt-wms/userWarehouse/fetchWarehouseForUserId?", "params": {"query": name, "userId": "164"} }, #fetch customer id { "url": "http://193.203.162.39:9090/nxt-wms/userCustomer/fetchCustomerForUserId?", "params": {"query": "TESTING 123", "userId": "164", "status": "Active"} }, #Stock summary based on warehouse id { "url": "http://193.203.162.39:9090/nxt-wms/transactionHistory/stockSummary?", "params": {"branchId": "343", "onDate": "2024-08-06", "warehouseId" : warehouse_id } "params": {"branchId": "343", "onDate": "2024-08-09", "warehouseId" : warehouse_id } }]
def inventory_report(question):
# Split the question to extract warehouse name, user question, and optional email parts = question.split(":", 2) name = parts[0].strip() user_question = parts[1].strip() user_email = parts[2].strip() if len(parts) > 2 else None print(f"Warehouse: {name}, Email: {user_email}, Question: {user_question}")
name = question.split(":")[0] #print(question) question = question.split(":")[1] #print(name) import requests
data = make_api_request(apis[0]["url"], apis[0]["params"]) if data:7 unmodified lines if "warehouseId" in api["params"]: api["params"]["warehouseId"] = warehouse_id
#print(apis[2]["url"]) #print(apis[2]["params"]) data1 = make_api_request(apis[2]["url"], apis[2]["params"]) #if data1: #print(data1)
data1 = make_api_request(apis[1]["url"], apis[1]["params"])
from tabulate import tabulate
21 unmodified lines table_data.append(row)
#if table_data: #print(tabulate(table_data, headers=headers, tablefmt="grid"))
# Convert to pandas DataFrame # Convert to pandas DataFrame import pandas as pd df = pd.DataFrame(table_data, columns=headers) import pandas as pd from pandasai.llm.openai import OpenAI from pandasai import SmartDataframe #open api key import openai llm = OpenAI() sdf = SmartDataframe(df, config={"llm": llm})
sdf = SmartDataframe(df, config={"llm": llm_chart})
#chart = sdf.chat("Can you draw a bar chart with all avaialble item name and quantity.") chart = sdf.chat(question)
#email send if user_email: # Send email with the chart image attached send_email_with_attachment( recipient_email=user_email, subject="Warehouse Inventory Report", body="Please find the attached bar chart report for the warehouse inventory analysis.", #attachment_path=chart_path attachment_path="/home/user/app/exports/charts/temp_chart.png" )
return chart#inventory_report("WH:can you give me a bar chart with item name and quantity for the warehouse WH")
35 unmodified lines name="dataVisualization", args_schema=QueryInput, output_schema=QueryOutput, description="Tool to generate visual output for a particular warehouse. Invoke this tool if the user wants to create charts. Process 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. " description = """ Tool to generate a visual output (such as a bar chart) for a particular warehouse based on the provided question. This tool processes the user question to identify the warehouse name and the specific request. If the user specifies an email, include the email in the input. The input format should be: 'warehouse name: user question: email (if any)'. The tool generates the requested chart and sends it to the provided email if specified.
Examples: 1. Question without email: "Analyze item name and quantity in a bar chart in warehouse Allcargo Logistics" Input to tool: "Allcargo Logistics: I want to analyze item name and quantity in a bar chart"
2. Question with email: "Analyze item name and quantity in a bar chart in warehouse Allcargo Logistics report to send email to example@example.com" Input to tool: "Allcargo Logistics: I want to analyze item name and quantity in a bar chart: example@example.com" """ )]
10 unmodified linesagent = create_tool_calling_agent(llm, tools, ChatPromptTemplate.from_template(prompt_template))agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
def ensure_temp_chart_dir(): temp_chart_dir = "/home/user/app/exports/charts/" if not os.path.exists(temp_chart_dir): os.makedirs(temp_chart_dir)
def clean_gradio_tmp_dir(): tmp_dir = "/tmp/gradio/" if os.path.exists(tmp_dir): try: shutil.rmtree(tmp_dir) except Exception as e: print(f"Error cleaning up /tmp/gradio/ directory: {e}")
# Define the interface functionmax_iterations = 5iterations = 0
def answer_question(user_question,chatbot):def answer_question(user_question, chatbot, audio=None): global iterations iterations = 0 # Ensure the temporary chart directory exists #ensure_temp_chart_dir() # Clean the /tmp/gradio/ directory #clean_gradio_tmp_dir() # Handle audio input if provided if audio is not None: sample_rate, audio_data = audio audio_segment = AudioSegment( audio_data.tobytes(), frame_rate=sample_rate, sample_width=audio_data.dtype.itemsize, channels=1 ) with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_audio_file: audio_segment.export(temp_audio_file.name, format="wav") temp_audio_file_path = temp_audio_file.name
recognizer = sr.Recognizer() with sr.AudioFile(temp_audio_file_path) as source: audio_content = recognizer.record(source) try: user_question = recognizer.recognize_google(audio_content) except sr.UnknownValueError: user_question = "Sorry, I could not understand the audio." except sr.RequestError: user_question = "Could not request results from Google Speech Recognition service."
while iterations < max_iterations: print(user_question) if "send email to" in user_question: email_match = re.search(r"send email to ([\w\.-]+@[\w\.-]+)", user_question) if email_match: user_email = email_match.group(1).strip() user_question = user_question.replace(f"send email to {user_email}", "").strip() user_question = f"{user_question}:{user_email}" #print(user_question) response = agent_executor.invoke({"input": user_question}) #print(response) if isinstance(response, dict): response_text = response.get("output", "") else: response_text = response if "invalid" not in response_text.lower(): break iterations += 1
response = agent_executor.invoke({"input": user_question})
if isinstance(response, dict): response_text = response.get("output", "") else: response_text = response if "invalid" not in response_text.lower(): break iterations += 1
if iterations == max_iterations: return "The agent could not generate a valid response within the iteration limit."11 unmodified lines #image = gr.Image(value=img_str) chatbot.append((user_question,img)) #print(chatbot) if "send email to" in user_question: try: os.remove(image_path) # Clean up the temporary image file except Exception as e: print(f"Error cleaning up image file: {e}") except Exception as e: print(f"Error loading image file: {e}") chatbot.append((user_question, "Chart generation failed. Please try again.")) else: chatbot.append((user_question, "Chart generation failed. Please try again.")) return gr.update(value=chatbot)
#return [(user_question,gr.Image("/home/user/app/exports/charts/temp_chart.png"))] # return "/home/user/app/exports/charts/temp_chart.png"47 unmodified lines with gr.Row(): with gr.Column(scale=1): message = gr.Textbox(show_label=False) audio_input = gr.Audio(label="Record your question") with gr.Column(scale=1): with gr.Row(): button = gr.Button("Submit", elem_classes="gr-button")20 unmodified lines demo.launch()
app.py+184-52