Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -25,63 +25,40 @@ cors = CORS(app)
|
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
-
class
|
| 29 |
def __init__(self, context):
|
| 30 |
super().__init__(context)
|
| 31 |
-
# Ensure the export directory exists
|
| 32 |
-
os.makedirs("/home/user/app/exports/charts", exist_ok=True)
|
| 33 |
|
| 34 |
def format_dataframe(self, result):
|
| 35 |
-
""
|
| 36 |
-
Convert a DataFrame to an image using dataframe_image,
|
| 37 |
-
and return a dict with type 'plot' to match the expected output.
|
| 38 |
-
"""
|
| 39 |
-
try:
|
| 40 |
-
df = result['value']
|
| 41 |
-
# Apply styling if desired
|
| 42 |
-
styled_df = df.style
|
| 43 |
-
img_path = f"/home/user/app/exports/charts/{uuid.uuid4().hex}.png"
|
| 44 |
-
dfi.export(styled_df, img_path)
|
| 45 |
-
except Exception as e:
|
| 46 |
-
print("Error in format_dataframe:", e)
|
| 47 |
-
# Fallback to a string representation if needed
|
| 48 |
-
img_path = str(result['value'])
|
| 49 |
-
print("response_class_path (dataframe):", img_path)
|
| 50 |
-
# Return as a dict with type 'plot'
|
| 51 |
-
return {'type': 'plot', 'value': img_path}
|
| 52 |
|
| 53 |
def format_plot(self, result):
|
| 54 |
-
|
| 55 |
-
# If
|
| 56 |
-
if hasattr(
|
| 57 |
try:
|
| 58 |
buf = io.BytesIO()
|
| 59 |
-
|
| 60 |
buf.seek(0)
|
| 61 |
image_base64 = base64.b64encode(buf.read()).decode("utf-8")
|
| 62 |
-
return
|
| 63 |
except Exception as e:
|
| 64 |
print("Error processing figure:", e)
|
| 65 |
-
return
|
| 66 |
-
# If
|
| 67 |
-
if isinstance(
|
| 68 |
-
|
| 69 |
-
|
|
|
|
| 70 |
data = file.read()
|
| 71 |
base64_data = base64.b64encode(data).decode("utf-8")
|
| 72 |
-
return
|
| 73 |
# Fallback: return as a string.
|
| 74 |
-
return
|
| 75 |
|
| 76 |
def format_other(self, result):
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
result.get('value').startswith('data:image')
|
| 80 |
-
):
|
| 81 |
-
return self.format_plot({'type': 'plot', 'value': result['value']})
|
| 82 |
-
|
| 83 |
-
return {'type': 'text', 'value': str(result['value'])}
|
| 84 |
-
|
| 85 |
|
| 86 |
|
| 87 |
gemini_api_key = os.getenv('Gemini')
|
|
|
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
+
class FlaskResponse(ResponseParser):
|
| 29 |
def __init__(self, context):
|
| 30 |
super().__init__(context)
|
|
|
|
|
|
|
| 31 |
|
| 32 |
def format_dataframe(self, result):
|
| 33 |
+
return result["value"].to_html()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
def format_plot(self, result):
|
| 36 |
+
val = result["value"]
|
| 37 |
+
# If val is a matplotlib figure, handle it accordingly.
|
| 38 |
+
if hasattr(val, "savefig"):
|
| 39 |
try:
|
| 40 |
buf = io.BytesIO()
|
| 41 |
+
val.savefig(buf, format="png")
|
| 42 |
buf.seek(0)
|
| 43 |
image_base64 = base64.b64encode(buf.read()).decode("utf-8")
|
| 44 |
+
return f"data:image/png;base64,{image_base64}"
|
| 45 |
except Exception as e:
|
| 46 |
print("Error processing figure:", e)
|
| 47 |
+
return str(val)
|
| 48 |
+
# If val is a string and is a valid file path, read and encode it.
|
| 49 |
+
if isinstance(val, str) and os.path.isfile(os.path.join(val)):
|
| 50 |
+
image_path = os.path.join(val)
|
| 51 |
+
print("My image path:", image_path)
|
| 52 |
+
with open(image_path, "rb") as file:
|
| 53 |
data = file.read()
|
| 54 |
base64_data = base64.b64encode(data).decode("utf-8")
|
| 55 |
+
return f"data:image/png;base64,{base64_data}"
|
| 56 |
# Fallback: return as a string.
|
| 57 |
+
return str(val)
|
| 58 |
|
| 59 |
def format_other(self, result):
|
| 60 |
+
# For non-image responses, simply return the value as a string.
|
| 61 |
+
return str(result["value"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
|
| 64 |
gemini_api_key = os.getenv('Gemini')
|