Update app.py
Browse files
app.py
CHANGED
|
@@ -6,27 +6,28 @@ import google.generativeai as genai
|
|
| 6 |
from PIL import Image
|
| 7 |
|
| 8 |
def process_file(api_key, file, instructions):
|
| 9 |
-
#
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
outputs = [gr.Image(label=f"Visualization {i+1}", width=600) for i in range(3)]
|
| 25 |
-
|
| 26 |
-
submit.click(
|
| 27 |
-
process_file,
|
| 28 |
-
inputs=[api_key, file, instructions],
|
| 29 |
-
outputs=outputs
|
| 30 |
-
)
|
| 31 |
-
|
| 32 |
-
demo.launch()
|
|
|
|
| 6 |
from PIL import Image
|
| 7 |
|
| 8 |
def process_file(api_key, file, instructions):
|
| 9 |
+
# Configure Gemini API
|
| 10 |
+
genai.configure(api_key=api_key)
|
| 11 |
+
model = genai.GenerativeModel('gemini-2.5-pro-latest')
|
| 12 |
|
| 13 |
+
# File handling with error prevention
|
| 14 |
+
try:
|
| 15 |
+
if file.name.endswith('.csv'):
|
| 16 |
+
df = pd.read_csv(file.name)
|
| 17 |
+
else:
|
| 18 |
+
df = pd.read_excel(file.name)
|
| 19 |
+
except Exception as e:
|
| 20 |
+
return [f"File Error: {str(e)}"] * 3
|
| 21 |
+
|
| 22 |
+
# Enhanced prompt template
|
| 23 |
+
prompt = f"""Generate exactly 3 distinct matplotlib visualizations for:
|
| 24 |
+
Columns: {list(df.columns)}
|
| 25 |
+
Data types: {dict(df.dtypes)}
|
| 26 |
+
Sample data: {df.head(3).to_dict()}
|
| 27 |
|
| 28 |
+
Requirements:
|
| 29 |
+
1. 1920x1080 resolution (figsize=(16,9), dpi=120)
|
| 30 |
+
2. Professional styling (seaborn, grid, proper labels)
|
| 31 |
+
3. Diverse chart types (include at least 1 advanced visualization)
|
| 32 |
|
| 33 |
+
User instructions: {instructions or 'None provided
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|