Sw1ft0 commited on
Commit
414da23
ยท
1 Parent(s): f499806

Add Gemini API. Set latest Gradio version.

Browse files
Files changed (3) hide show
  1. README.md +3 -3
  2. app.py +63 -39
  3. 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.39.0
8
  app_file: app.py
9
  pinned: false
10
  ---
11
 
12
  # ๐Ÿ“Š Excel AI Analyzer
13
 
14
- Upload an Excel file and ask questions about revenue and occupancy.
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
- def upload_excel(file):
8
- if file is None:
9
- return "Please upload an Excel file first."
 
 
 
 
10
  try:
11
- df = pd.read_excel(file.name)
12
- dataframes["df"] = df
13
- preview = df.head().to_string()
14
- return f"โœ… File uploaded successfully! Here are the first rows:\n\n{preview}"
15
- except Exception as e:
16
- return f"โŒ Error reading Excel file: {e}"
17
 
18
- def chat_agent(message, history):
19
  if "df" not in dataframes:
20
- return "Please upload an Excel file first."
21
-
22
  df = dataframes["df"]
 
 
 
 
 
 
 
 
 
23
 
24
- # Very simple โ€œquestion routingโ€ demo
25
- message_lower = message.lower()
26
-
27
- if "otb" in message_lower and "revenue" in message_lower and "august" in message_lower:
28
- try:
29
- otb_august = df.loc[df['Month'] == 'August', 'OTB Revenue'].values[0]
30
- stly_august = df.loc[df['Month'] == 'August', 'STLY Revenue'].values[0]
31
- return f"OTB Revenue (August): {otb_august}\nSTLY Revenue (August): {stly_august}"
32
- except:
33
- return "I couldn't find OTB Revenue data for August in your file."
34
 
35
- elif "occupancy" in message_lower and "august" in message_lower:
36
- try:
37
- occ_actual = df.loc[df['Month'] == 'August', 'Occupancy %'].values[0]
38
- occ_target = df.loc[df['Month'] == 'August', 'Budgeted Occupancy %'].values[0]
39
- if occ_actual >= occ_target:
40
- return f"Occupancy for August is {occ_actual}% vs target {occ_target}% โœ… On track!"
41
- else:
42
- return f"Occupancy for August is {occ_actual}% vs target {occ_target}% โš ๏ธ Might not meet target."
43
- except:
44
- return "I couldn't find occupancy data for August in your file."
 
 
 
 
 
45
 
46
- else:
47
- return "Sorry, I can only answer about OTB Revenue and Occupancy for now. (You can extend me with more rules!)"
48
 
 
 
 
 
 
 
 
49
 
 
 
 
50
  with gr.Blocks() as demo:
51
- gr.Markdown("# ๐Ÿ“Š OtelAI Excel Q&A Agent")
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="file")
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
- gradio
 
 
1
+ gradio==4.44.1
2
  pandas
3
  openpyxl
4
+ langchain
5
+ langchain-google-genai