Spaces:
Sleeping
Sleeping
Sw1ft0
commited on
Commit
ยท
414da23
1
Parent(s):
f499806
Add Gemini API. Set latest Gradio version.
Browse files- README.md +3 -3
- app.py +63 -39
- requirements.txt +3 -1
README.md
CHANGED
|
@@ -1,15 +1,15 @@
|
|
| 1 |
---
|
|
|
|
| 2 |
title: Excel AI Analyzer
|
| 3 |
emoji: ๐
|
| 4 |
colorFrom: indigo
|
| 5 |
colorTo: blue
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version: 4.
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
|
| 12 |
# ๐ Excel AI Analyzer
|
| 13 |
|
| 14 |
-
|
| 15 |
-
Built with **Gradio** + **pandas**.
|
|
|
|
| 1 |
---
|
| 2 |
+
# This is the Hugging Face Space config block
|
| 3 |
title: Excel AI Analyzer
|
| 4 |
emoji: ๐
|
| 5 |
colorFrom: indigo
|
| 6 |
colorTo: blue
|
| 7 |
sdk: gradio
|
| 8 |
+
sdk_version: 4.44.1
|
| 9 |
app_file: app.py
|
| 10 |
pinned: false
|
| 11 |
---
|
| 12 |
|
| 13 |
# ๐ Excel AI Analyzer
|
| 14 |
|
| 15 |
+
This application allows users to upload an Excel file, analyze its contents, and interact with the data through a chat interface powered by Google's Gemini API.
|
|
|
app.py
CHANGED
|
@@ -1,61 +1,85 @@
|
|
| 1 |
import pandas as pd
|
| 2 |
import gradio as gr
|
|
|
|
|
|
|
| 3 |
|
| 4 |
# Store the uploaded dataframe in memory
|
| 5 |
dataframes = {}
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
try:
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
return f"โ Error reading Excel file: {e}"
|
| 17 |
|
| 18 |
-
def
|
| 19 |
if "df" not in dataframes:
|
| 20 |
-
return "Please upload
|
| 21 |
-
|
| 22 |
df = dataframes["df"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
return "I couldn't find OTB Revenue data for August in your file."
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
|
|
|
|
|
|
|
|
|
| 50 |
with gr.Blocks() as demo:
|
| 51 |
-
gr.Markdown("#
|
| 52 |
-
gr.Markdown("Upload an Excel report and then ask questions about OTB revenue or occupancy.")
|
| 53 |
-
|
| 54 |
with gr.Row():
|
| 55 |
with gr.Column():
|
| 56 |
-
file_input = gr.File(label="Upload Excel file", type="
|
| 57 |
upload_output = gr.Textbox(label="Upload status")
|
| 58 |
-
|
| 59 |
file_input.upload(upload_excel, file_input, upload_output)
|
| 60 |
|
| 61 |
gr.ChatInterface(fn=chat_agent, title="Excel Chat Agent")
|
|
|
|
| 1 |
import pandas as pd
|
| 2 |
import gradio as gr
|
| 3 |
+
from langchain.agents import initialize_agent, Tool
|
| 4 |
+
from langchain_google_genai import ChatGoogleGenerativeAI # requires GOOGLE_API_KEY set as env var
|
| 5 |
|
| 6 |
# Store the uploaded dataframe in memory
|
| 7 |
dataframes = {}
|
| 8 |
|
| 9 |
+
# -----------------------------
|
| 10 |
+
# Tools (Excel Q&A Functions)
|
| 11 |
+
# -----------------------------
|
| 12 |
+
def get_otb_revenue(month: str) -> str:
|
| 13 |
+
if "df" not in dataframes:
|
| 14 |
+
return "Please upload a file first."
|
| 15 |
+
df = dataframes["df"]
|
| 16 |
try:
|
| 17 |
+
otb = df.loc[df['Month'].str.lower() == month.lower(), 'OTB Revenue'].values[0]
|
| 18 |
+
stly = df.loc[df['Month'].str.lower() == month.lower(), 'STLY Revenue'].values[0]
|
| 19 |
+
return f"OTB Revenue for {month}: {otb}, STLY Revenue: {stly}"
|
| 20 |
+
except Exception:
|
| 21 |
+
return f"Could not find OTB/ STLY Revenue for {month}"
|
|
|
|
| 22 |
|
| 23 |
+
def check_occupancy(month: str) -> str:
|
| 24 |
if "df" not in dataframes:
|
| 25 |
+
return "Please upload a file first."
|
|
|
|
| 26 |
df = dataframes["df"]
|
| 27 |
+
try:
|
| 28 |
+
occ = df.loc[df['Month'].str.lower() == month.lower(), 'Occupancy %'].values[0]
|
| 29 |
+
target = df.loc[df['Month'].str.lower() == month.lower(), 'Budgeted Occupancy %'].values[0]
|
| 30 |
+
if occ >= target:
|
| 31 |
+
return f"Occupancy for {month} is {occ}% vs target {target}% โ
On track!"
|
| 32 |
+
else:
|
| 33 |
+
return f"Occupancy for {month} is {occ}% vs target {target}% โ ๏ธ Might not meet target."
|
| 34 |
+
except Exception:
|
| 35 |
+
return f"Could not find occupancy data for {month}"
|
| 36 |
|
| 37 |
+
# -----------------------------
|
| 38 |
+
# Upload Excel
|
| 39 |
+
# -----------------------------
|
| 40 |
+
def upload_excel(file):
|
| 41 |
+
if file is None:
|
| 42 |
+
return "Please upload an Excel file."
|
| 43 |
+
df = pd.read_excel(file.name)
|
| 44 |
+
dataframes["df"] = df
|
| 45 |
+
return f"โ
File loaded with {len(df)} rows and {len(df.columns)} columns."
|
|
|
|
| 46 |
|
| 47 |
+
# -----------------------------
|
| 48 |
+
# LangChain Agent Setup
|
| 49 |
+
# -----------------------------
|
| 50 |
+
tools = [
|
| 51 |
+
Tool(
|
| 52 |
+
name="Get OTB Revenue",
|
| 53 |
+
func=get_otb_revenue,
|
| 54 |
+
description="Get OTB Revenue and STLY Revenue for a given month (e.g. 'August')"
|
| 55 |
+
),
|
| 56 |
+
Tool(
|
| 57 |
+
name="Check Occupancy",
|
| 58 |
+
func=check_occupancy,
|
| 59 |
+
description="Check occupancy vs target for a given month (e.g. 'August')"
|
| 60 |
+
)
|
| 61 |
+
]
|
| 62 |
|
| 63 |
+
llm = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0) # Requires GOOGLE_API_KEY in environment
|
| 64 |
+
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)
|
| 65 |
|
| 66 |
+
def chat_agent(message, history):
|
| 67 |
+
if "df" not in dataframes:
|
| 68 |
+
return "Please upload a file first."
|
| 69 |
+
try:
|
| 70 |
+
return agent.run(message)
|
| 71 |
+
except Exception as e:
|
| 72 |
+
return f"โ ๏ธ Agent error: {e}"
|
| 73 |
|
| 74 |
+
# -----------------------------
|
| 75 |
+
# Gradio UI
|
| 76 |
+
# -----------------------------
|
| 77 |
with gr.Blocks() as demo:
|
| 78 |
+
gr.Markdown("# ๐ค Excel Agent (LangChain + Gemini)")
|
|
|
|
|
|
|
| 79 |
with gr.Row():
|
| 80 |
with gr.Column():
|
| 81 |
+
file_input = gr.File(label="Upload Excel file", type="filepath")
|
| 82 |
upload_output = gr.Textbox(label="Upload status")
|
|
|
|
| 83 |
file_input.upload(upload_excel, file_input, upload_output)
|
| 84 |
|
| 85 |
gr.ChatInterface(fn=chat_agent, title="Excel Chat Agent")
|
requirements.txt
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
|
|
| 1 |
pandas
|
| 2 |
openpyxl
|
| 3 |
-
|
|
|
|
|
|
| 1 |
+
gradio==4.44.1
|
| 2 |
pandas
|
| 3 |
openpyxl
|
| 4 |
+
langchain
|
| 5 |
+
langchain-google-genai
|