Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,8 +4,8 @@ import pandas as pd
|
|
| 4 |
import numpy as np
|
| 5 |
import requests
|
| 6 |
import json
|
| 7 |
-
import time
|
| 8 |
-
import matplotlib.pyplot as plt # Explicitly
|
| 9 |
|
| 10 |
# --- CONFIG ---
|
| 11 |
# Note: GEMINI_API_KEY is retrieved from environment variables/secrets.
|
|
@@ -17,7 +17,8 @@ if not GEMINI_API_KEY:
|
|
| 17 |
|
| 18 |
# Define API endpoints and models
|
| 19 |
GEMINI_BASE = "https://generativelanguage.googleapis.com/v1beta"
|
| 20 |
-
|
|
|
|
| 21 |
EMBED_MODEL = "models/embedding-001"
|
| 22 |
|
| 23 |
# Define the JSON schema for structured output
|
|
@@ -52,7 +53,8 @@ SYSTEM_INSTRUCTION = (
|
|
| 52 |
def chat_with_gemini(prompt, context):
|
| 53 |
"""Sends a prompt and data context to the Gemini model for structured analysis (reasoning + code)."""
|
| 54 |
|
| 55 |
-
|
|
|
|
| 56 |
|
| 57 |
# Construct the full prompt including the data context
|
| 58 |
full_prompt = f"Data Context (DataFrame Head and Columns):\n{context}\n\nUser Question: {prompt}"
|
|
@@ -73,7 +75,7 @@ def chat_with_gemini(prompt, context):
|
|
| 73 |
for attempt in range(max_retries):
|
| 74 |
try:
|
| 75 |
r = requests.post(url, headers={'Content-Type': 'application/json'}, data=json.dumps(payload))
|
| 76 |
-
r.raise_for_status()
|
| 77 |
data = r.json()
|
| 78 |
|
| 79 |
# The JSON output is a string inside the 'text' part
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
import requests
|
| 6 |
import json
|
| 7 |
+
import time
|
| 8 |
+
import matplotlib.pyplot as plt # Explicitly imported for use in exec() scope
|
| 9 |
|
| 10 |
# --- CONFIG ---
|
| 11 |
# Note: GEMINI_API_KEY is retrieved from environment variables/secrets.
|
|
|
|
| 17 |
|
| 18 |
# Define API endpoints and models
|
| 19 |
GEMINI_BASE = "https://generativelanguage.googleapis.com/v1beta"
|
| 20 |
+
# Using the correct model for structured output
|
| 21 |
+
CHAT_MODEL = "gemini-2.5-flash-preview-09-2025"
|
| 22 |
EMBED_MODEL = "models/embedding-001"
|
| 23 |
|
| 24 |
# Define the JSON schema for structured output
|
|
|
|
| 53 |
def chat_with_gemini(prompt, context):
|
| 54 |
"""Sends a prompt and data context to the Gemini model for structured analysis (reasoning + code)."""
|
| 55 |
|
| 56 |
+
# FIX: Correctly prepend 'models/' to the model name in the URL path
|
| 57 |
+
url = f"{GEMINI_BASE}/models/{CHAT_MODEL}:generateContent?key={GEMINI_API_KEY}"
|
| 58 |
|
| 59 |
# Construct the full prompt including the data context
|
| 60 |
full_prompt = f"Data Context (DataFrame Head and Columns):\n{context}\n\nUser Question: {prompt}"
|
|
|
|
| 75 |
for attempt in range(max_retries):
|
| 76 |
try:
|
| 77 |
r = requests.post(url, headers={'Content-Type': 'application/json'}, data=json.dumps(payload))
|
| 78 |
+
r.raise_for_status() # This will raise an exception for 4xx or 5xx errors
|
| 79 |
data = r.json()
|
| 80 |
|
| 81 |
# The JSON output is a string inside the 'text' part
|