Spaces:
Sleeping
Sleeping
import gradio as gr | |
import requests | |
import pandas as pd | |
from langchain.chat_models import ChatOpenAI | |
from langchain.document_loaders import CSVLoader | |
from langchain_together import TogetherEmbeddings | |
from langchain.prompts import ChatPromptTemplate | |
from langchain.vectorstores import Chroma | |
from langchain_core.output_parsers import StrOutputParser | |
from langchain_core.runnables import RunnableLambda, RunnablePassthrough | |
from langchain.document_loaders import CSVLoader | |
from langchain.embeddings.sentence_transformer import SentenceTransformerEmbeddings | |
from langchain.vectorstores import Chroma | |
from langchain_core.vectorstores import InMemoryVectorStore | |
from langchain import PromptTemplate | |
from langchain import LLMChain | |
from langchain_together import Together | |
import os | |
os.environ['TOGETHER_API_KEY'] = "c2f52626b97118b71c0c36f66eda4f5957c8fc475e760c3d72f98ba07d3ed3b5" | |
# Initialize global variable for vectorstore | |
vectorstore = None | |
embeddings = TogetherEmbeddings(model="togethercomputer/m2-bert-80M-8k-retrieval") | |
llama3 = Together(model="meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo", max_tokens=1024) | |
def update_csv_files(): | |
# Define the login URL and credentials | |
login_url = "https://livesystem.hisabkarlay.com/auth/login" | |
payload = { | |
"username": "user@123", | |
"password": "user@123", | |
"client_secret": "kNqJjlPkxyHdIKt3szCt4PYFWtFOdUheb8QVN8vQ", | |
"client_id": "5", | |
"grant_type": "password" | |
} | |
# Send a POST request to the login URL | |
response = requests.post(login_url, data=payload) | |
# Check the status and get the response data | |
if response.status_code == 200: | |
access_token = response.json()['access_token'] | |
else: | |
return f"Failed to log in: {response.status_code}" | |
# Profit loss Fetch report | |
report_url = "https://livesystem.hisabkarlay.com/connector/api/profit-loss-report" | |
headers = { | |
"Authorization": f"Bearer {access_token}" | |
} | |
response = requests.get(report_url, headers=headers) | |
profit_loss_data = response.json()['data'] | |
keys = list(profit_loss_data.keys()) | |
del keys[23] # Adjust according to your needs | |
del keys[20] | |
del keys[19] | |
data_dict = {} | |
for key in keys: | |
data_dict[key] = profit_loss_data.get(key) | |
df = pd.DataFrame(data_dict, index=[0]) | |
df.to_csv('profit_loss.csv', index=False) | |
# API call to get purchase-sell data | |
report_url = "https://livesystem.hisabkarlay.com/connector/api/purchase-sell" | |
response = requests.get(report_url, headers=headers) | |
sell_purchase_data = response.json() | |
sell_purchase_data = dict(list(sell_purchase_data.items())[2:]) | |
df = pd.json_normalize(sell_purchase_data) | |
df.to_csv('purchase_sell_report.csv', index=False) | |
# API call to get trending product data | |
report_url = "https://livesystem.hisabkarlay.com/connector/api/trending-products" | |
response = requests.get(report_url, headers=headers) | |
trending_product_data = response.json()['data'] | |
df = pd.DataFrame(trending_product_data) | |
df.columns = ['Product Units Sold', 'Product Name', 'Unit Type', 'SKU (Stock Keeping Unit)'] | |
df.to_csv('trending_product.csv', index=False) | |
return "CSV files updated successfully!" | |
def initialize_embedding(): | |
global vectorstore | |
# Initialize the embedding function | |
# Load CSV files | |
file_paths = [ | |
"profit_loss.csv", | |
"purchase_sell_report.csv", | |
"trending_product.csv" | |
] | |
documents = [] | |
for path in file_paths: | |
loader = CSVLoader(path, encoding="windows-1252") | |
documents.extend(loader.load()) # Combine documents from all files | |
# Create an InMemoryVectorStore from the combined documents | |
vectorstore = InMemoryVectorStore.from_texts( | |
[doc.page_content for doc in documents], # Extract the page_content from Document objects | |
embedding=embeddings, | |
) | |
return "Embeddings initialized successfully!" | |
def qa_chain(query): | |
if vectorstore is None: | |
return "Please initialize the embeddings first." | |
retriever = vectorstore.as_retriever() | |
retrieved_documents = retriever.invoke(query) | |
return retrieved_documents # Not shown directly in the UI | |
def generate_response(query, history): | |
if vectorstore is None: | |
return history, "Please initialize the embeddings first." | |
retrieved_documents = qa_chain(query) # Call qa_chain internally | |
chat_template = """ | |
You are a highly intelligent and professional AI assistant. | |
Generate the response according to the user's query: | |
- If the user enters a greeting (e.g., "Hi", "Hello", "Good day"), give the following response: | |
"Welcome to HisabKarLay, your business partner! You may choose from the following services π: | |
1. Reports | |
2. Forecasts | |
3. Best Selling Items | |
4. Chat with AI Agent | |
5. Chat with our Customer Care Team | |
6. Share your Feedback | |
7. Checkout Latest Offers | |
π Suggestion: To make a selection, send the relevant number like 1 | |
β Note: If at any stage you wish to go back to the previous menu, type back, and to go to the main menu, type main menu. | |
β Note: If you want to change the language, type and send 'change language.' | |
ππ»βοΈ Help: If you need any help, you can call us at +923269498569." | |
- If the user enters a specific number (1-7), give the following responses... | |
""" | |
prompt = PromptTemplate( | |
input_variables=['retrieved_documents', 'query'], | |
template=chat_template | |
) | |
Generated_chat = LLMChain(llm=llama3, prompt=prompt) | |
result = Generated_chat.run({ | |
"retrieved_documents": retrieved_documents, | |
"query": query | |
}) | |
# Append the conversation history | |
history.append((query, result)) | |
return history, result | |
# Define Gradio UI | |
with gr.Blocks() as demo: | |
chatbot = gr.Chatbot(label="AI Chat") | |
query = gr.Textbox(label="Ask anything!", placeholder="Type your question here") | |
initialize_status = gr.Textbox(label="Status", visible=False) | |
update_csv_status = gr.Textbox(label="Status", visible=False) | |
initialize_button = gr.Button("Initialize Embeddings") | |
update_csv_button = gr.Button("Update CSV Files") | |
def on_query(query, history): | |
return generate_response(query, history) | |
query.submit(on_query, [query, chatbot], [chatbot, query]) | |
initialize_button.click(initialize_embedding, outputs=initialize_status) | |
update_csv_button.click(update_csv_files, outputs=update_csv_status) | |
# Launch Gradio App | |
demo.launch() | |