Spaces:
Build error
Build error
Update app.py
Browse files211 unmodified lines21221321421521621710 unmodified lines2282292302312322339 unmodified lines2432442452462472485 unmodified lines254255256257258259211 unmodified linesif product_name:attributes["Product name"] = product_name# Convert attributes to DataFrame for editingdf = pd.DataFrame(list(attributes.items()), columns=["Attribute", "Value"])return f"Extracted Text:\n{extracted_text}", df, None10 unmodified linesexcept Exception as e:return f"β Error exporting to Salesforce: {str(e)}"# Gradio Interfacedef app():with gr.Blocks() as demo:9 unmodified linesok_button = gr.Button("OK")result_output = gr.Text(label="π Result")# Define button actionsextract_button.click(fn=process_image,5 unmodified linesinputs=[mode_input, entry_type_input, quantity_input, editable_df_output],outputs=[result_output])return demo211 unmodified lines21221321421521621721821922022122210 unmodified lines2332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882899 unmodified lines2993003013023033043053063073083093103115 unmodified lines317318319320321322323324325326327211 unmodified linesif product_name:attributes["Product name"] = product_name# Ensure fixed attributes are presentfor fixed_attr in ["Stage", "H.P.", "Product name", "Model"]:if fixed_attr not in attributes:attributes[fixed_attr] = ""# Convert attributes to DataFrame for editingdf = pd.DataFrame(list(attributes.items()), columns=["Attribute", "Value"])return f"Extracted Text:\n{extracted_text}", df, None10 unmodified linesexcept Exception as e:return f"β Error exporting to Salesforce: {str(e)}"# Function to pull structured data from Salesforce and display as a tabledef pull_data_from_salesforce(data_type):try:sf = Salesforce(username=SALESFORCE_USERNAME,password=SALESFORCE_PASSWORD,security_token=SALESFORCE_SECURITY_TOKEN)if data_type == "Inventory":query = "SELECT Productname__c, Current_Stocks__c, soldstock__c FROM Inventory_Management__c LIMIT 100"else:query = "SELECT Productname__c, Current_Stock__c, soldstock__c FROM Un_Billable__c LIMIT 100"response = sf.query_all(query)records = response.get("records", [])if not records:return "No data found in Salesforce.", None, None, Nonedf = pd.DataFrame(records)df = df.drop(columns=['attributes'], errors='ignore')# Rename columns for better readabilitydf.rename(columns={"Productname__c": "Product Name","Current_Stocks__c": "Current Stocks","Current_Stock__c": "Current Stocks","soldstock__c": "Sold Stock"}, inplace=True)excel_path = "salesforce_data.xlsx"df.to_excel(excel_path, index=False)# Generate interactive vertical bar graph using Matplotlibfig, ax = plt.subplots(figsize=(12, 8))df.plot(kind='bar', x="Product Name", y="Current Stocks", ax=ax, legend=False)ax.set_title("Stock Distribution by Product Name")ax.set_xlabel("Product Name")ax.set_ylabel("Current Stocks")plt.xticks(rotation=45, ha="right", fontsize=10)plt.tight_layout()buffer = BytesIO()plt.savefig(buffer, format="png")buffer.seek(0)img = Image.open(buffer)return df, excel_path, imgexcept Exception as e:return f"Error fetching data: {str(e)}", None, None, None# Gradio Interfacedef app():with gr.Blocks() as demo:9 unmodified linesok_button = gr.Button("OK")result_output = gr.Text(label="π Result")with gr.Tab("π Salesforce Data"):data_type_input = gr.Dropdown(label="Select Data Type", choices=["Inventory", "Unbilling"], value="Inventory")pull_button = gr.Button("Pull Data from Salesforce")salesforce_data_output = gr.Dataframe(label="π Salesforce Data")excel_download_output = gr.File(label="π₯ Download Excel")graph_output = gr.Image(label="π Stock Distribution Graph")# Define button actionsextract_button.click(fn=process_image,5 unmodified linesinputs=[mode_input, entry_type_input, quantity_input, editable_df_output],outputs=[result_output])pull_button.click(fn=pull_data_from_salesforce,inputs=[data_type_input],outputs=[salesforce_data_output, excel_download_output, graph_output])return demo
app.py+68