FatimaGr commited on
Commit
dfcb47b
·
verified ·
1 Parent(s): df259fb
Files changed (1) hide show
  1. app.py +4 -7
app.py CHANGED
@@ -194,14 +194,13 @@ print("hello")
194
  async def generate_viz(file: UploadFile = File(...), query: str = Form(...)):
195
  print("hello")
196
  try:
197
- contents = await file.read() # 👈 Lire tout le contenu du fichier
198
- excel_file = io.BytesIO(contents) # 👈 Le convertir en fichier binaire en mémoire
199
- df = pd.read_excel(excel_file) # 👈 Pandas lit depuis le BytesIO
200
-
201
  if query not in VALID_PLOTS:
202
  return JSONResponse(content={"error": f"Type de graphique invalide. Choisissez parmi : {', '.join(VALID_PLOTS)}"}, status_code=400)
203
 
204
- df = pd.read_excel(file.file)
205
  numeric_cols = df.select_dtypes(include=["number"]).columns
206
 
207
  if len(numeric_cols) < 1:
@@ -231,7 +230,6 @@ plt.close()
231
  outputs = codegen_model.generate(**inputs, max_new_tokens=120, pad_token_id=codegen_tokenizer.eos_token_id)
232
  generated_code = codegen_tokenizer.decode(outputs[0], skip_special_tokens=True).strip()
233
 
234
- # Nettoyage
235
  generated_code = re.sub(r"(import matplotlib.pyplot as plt\nimport seaborn as sns\n)+", "import matplotlib.pyplot as plt\nimport seaborn as sns\n", generated_code)
236
  if generated_code.strip().endswith("sns."):
237
  generated_code = generated_code.rsplit("\n", 1)[0]
@@ -252,7 +250,6 @@ plt.close()
252
  if os.path.getsize(img_path) == 0:
253
  return JSONResponse(content={"error": "Le fichier plot.png est vide."}, status_code=500)
254
 
255
- # Encoder l'image en base64
256
  with open(img_path, "rb") as image_file:
257
  encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
258
  return JSONResponse(content={"image_base64": encoded_string})
 
194
  async def generate_viz(file: UploadFile = File(...), query: str = Form(...)):
195
  print("hello")
196
  try:
197
+ contents = await file.read()
198
+ excel_file = io.BytesIO(contents)
199
+ df = pd.read_excel(excel_file)
200
+
201
  if query not in VALID_PLOTS:
202
  return JSONResponse(content={"error": f"Type de graphique invalide. Choisissez parmi : {', '.join(VALID_PLOTS)}"}, status_code=400)
203
 
 
204
  numeric_cols = df.select_dtypes(include=["number"]).columns
205
 
206
  if len(numeric_cols) < 1:
 
230
  outputs = codegen_model.generate(**inputs, max_new_tokens=120, pad_token_id=codegen_tokenizer.eos_token_id)
231
  generated_code = codegen_tokenizer.decode(outputs[0], skip_special_tokens=True).strip()
232
 
 
233
  generated_code = re.sub(r"(import matplotlib.pyplot as plt\nimport seaborn as sns\n)+", "import matplotlib.pyplot as plt\nimport seaborn as sns\n", generated_code)
234
  if generated_code.strip().endswith("sns."):
235
  generated_code = generated_code.rsplit("\n", 1)[0]
 
250
  if os.path.getsize(img_path) == 0:
251
  return JSONResponse(content={"error": "Le fichier plot.png est vide."}, status_code=500)
252
 
 
253
  with open(img_path, "rb") as image_file:
254
  encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
255
  return JSONResponse(content={"image_base64": encoded_string})