Spaces:
Sleeping
Sleeping
| import os | |
| from paddleocr import PaddleOCR | |
| from PIL import Image | |
| import gradio as gr | |
| import requests | |
| import re | |
| from simple_salesforce import Salesforce | |
| import pandas as pd | |
| import matplotlib.pyplot as plt | |
| from io import BytesIO | |
| from fuzzywuzzy import process | |
| # Salesforce credentials | |
| SALESFORCE_USERNAME = "venkatramana@sandbox.com" | |
| SALESFORCE_PASSWORD = "Venkat12345@" | |
| SALESFORCE_SECURITY_TOKEN = "GhcJJmjBEefdnukJoz4CAQlR" | |
| # Initialize PaddleOCR | |
| ocr = PaddleOCR(use_angle_cls=True, lang='en') | |
| # Function to extract text using PaddleOCR | |
| def extract_text(image): | |
| result = ocr.ocr(image) | |
| extracted_text = [] | |
| for line in result[0]: | |
| extracted_text.append(line[1][0]) | |
| return "\n".join(extracted_text) | |
| # Function to fetch data from Salesforce and format it into a DataFrame | |
| def fetch_salesforce_data(): | |
| try: | |
| sf = Salesforce( | |
| username=SALESFORCE_USERNAME, | |
| password=SALESFORCE_PASSWORD, | |
| security_token=SALESFORCE_SECURITY_TOKEN | |
| ) | |
| query = "SELECT Product_Name__c, Modal_Name__c, Current_Stocks__c FROM MotorDataAPI__c" | |
| response = sf.query_all(query) | |
| records = response["records"] | |
| df = pd.DataFrame(records) | |
| df = df.rename(columns={ | |
| "Product_Name__c": "Product Name", | |
| "Modal_Name__c": "Model Name", | |
| "Current_Stocks__c": "Current Stocks" | |
| }) | |
| return df[["Product Name", "Model Name", "Current Stocks"]] | |
| except Exception as e: | |
| return None | |
| # Function to generate a bar graph from Salesforce data | |
| def generate_salesforce_bar_chart(df): | |
| try: | |
| plt.figure(figsize=(12, 6)) | |
| df.plot(kind='bar', x="Product Name", y="Current Stocks", legend=False) | |
| plt.title("Stock Distribution by Product Name") | |
| plt.xlabel("Product Name") | |
| plt.ylabel("Current Stocks") | |
| plt.xticks(rotation=45, ha="right") | |
| plt.tight_layout() | |
| buffer = BytesIO() | |
| plt.savefig(buffer, format="png") | |
| buffer.seek(0) | |
| return Image.open(buffer) | |
| except Exception as e: | |
| return None | |
| # Unified function to handle image processing and Salesforce interaction | |
| def process_image(image, mode, entry_type, quantity): | |
| extracted_text = extract_text(image) | |
| if not extracted_text: | |
| return "No text detected in the image.", None | |
| return f"Extracted Text:\n{extracted_text}\n\nProcessing Completed.", None | |
| # Function to fetch and return Salesforce data | |
| def get_salesforce_visualization(): | |
| df = fetch_salesforce_data() | |
| if df is not None: | |
| table_component = df.to_html(index=False) | |
| bar_chart_image = generate_salesforce_bar_chart(df) | |
| return table_component, bar_chart_image | |
| return "<p>No Data Available</p>", None | |
| def app(): | |
| with gr.Blocks() as interface: | |
| gr.Markdown("<h1>π’ VENKATARAMANA MOTORS</h1>") | |
| with gr.Tab("π₯ Stock Entry & Processing"): | |
| with gr.Row(): | |
| with gr.Column(): | |
| gr.Markdown("<h3>π Upload & Process</h3>") | |
| image_input = gr.Image(type="numpy", label="π Upload Image") | |
| mode_dropdown = gr.Dropdown(label="π Mode", choices=["Entry", "Exit"], value="Entry") | |
| entry_type_radio = gr.Radio(label="π¦ Entry Type", choices=["Sales", "Non-Sales"], value="Sales") | |
| quantity_input = gr.Number(label="π’ Quantity", value=1, interactive=True) | |
| with gr.Column(): | |
| gr.Markdown("<h3>π Processed Data</h3>") | |
| image_view = gr.Text(label="π Image Data Viewer", interactive=False) | |
| result_output = gr.Text(label="π Processed Result", interactive=False) | |
| submit_button = gr.Button("π Process Image") | |
| with gr.Tab("π Salesforce Data Overview"): | |
| gr.Markdown("<h3>π Stock Table</h3>") | |
| with gr.Row(): | |
| salesforce_table = gr.HTML(label="π¦ Salesforce Data Table") | |
| gr.Markdown("<h3>π Inventory Analytics</h3>") | |
| with gr.Row(): | |
| salesforce_graph = gr.Image(type="pil", label="π Stock Distribution Bar Graph") | |
| generate_button = gr.Button("β‘ Generate Data") | |
| generate_button.click(fn=get_salesforce_visualization, inputs=[], outputs=[salesforce_table, salesforce_graph]) | |
| submit_button.click(fn=process_image, inputs=[image_input, mode_dropdown, entry_type_radio, quantity_input], outputs=[image_view, result_output]) | |
| return interface | |
| if __name__ == "__main__": | |
| app().launch(share=True) |