File size: 2,940 Bytes
64efae0
90d0a81
30b123a
46bcd76
6915dbd
5add6b6
a78bdf5
826da72
5add6b6
2cb9c9a
ab35ae7
2cb9c9a
 
 
 
04adfa1
 
46bcd76
 
 
 
 
 
 
 
6915dbd
2cb9c9a
 
 
2895d9c
 
 
2cb9c9a
d24bd61
 
 
46bcd76
 
 
 
 
5add6b6
 
46bcd76
 
5add6b6
46bcd76
2cb9c9a
 
 
64efae0
30b123a
64efae0
 
 
 
 
cfefc6c
64efae0
 
cfefc6c
64efae0
30b123a
 
2895d9c
1c1ce3e
e2d6933
 
 
 
 
 
1c1ce3e
e2d6933
a7223db
 
2cb9c9a
2c6fad5
e2d6933
 
2c6fad5
b4228bd
 
2c6fad5
 
 
 
3b75522
e2d6933
 
30b123a
a7223db
64efae0
 
 
f1a99bf
64efae0
2895d9c
 
 
a9f5a47
2895d9c
dd51fe9
515438f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
from fastapi import FastAPI, Request
from fastapi.responses import Response
from fastapi.responses import FileResponse
from pydantic import BaseModel

import random
import re
import numpy as np

import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
import pandas as pd

import io

app = FastAPI()

# Define the ticket schema using Pydantic
class Ticket(BaseModel):
    name: str
    department: str
    category: str
    description: str
    service_category: str
    difficulty: int  # Adjust type as needed (e.g., int or str)


class Code(BaseModel):
    code: str
    project_id: str
    chain_id: str
    session_id: str

@app.get("/")
def greet_json():
    return {"Hello": "World!"}

@app.post("/ticket")
async def create_ticket(ticket: Ticket):
    # Here you can process the ticket, e.g., save it to a database.
    # For now, we simply return the received ticket data.
    tick = ticket.dict()
    tick["number"] = random.randint(1000, 9999)
    return {
        "message": "Ticket created successfully",
        "ticket": tick
    }


@app.post("/run_code")
async def run_code(code: Code, request: Request):
    # img_buffer = io.BytesIO()

    pattern = f"""```python([\s\S]*?)```"""
    codes = re.findall(pattern, code.code)

    if codes:
        print(codes)
        exec("\n".join(codes))
    else:
        print(code.code)
        exec(code.code)
    # img_buffer.seek(0)  # Reset buffer position

    file_path = f"graphs_{code.project_id}_{code.chain_id}_{code.session_id}.pdf"

    # if "plt.subplots(" in code.code:
    #     print("SUBPLOTS DETECTED")
    #     exec(code.code + "\nfig.savefig(file_path)\nplt.close()")
    # else:
    #     print("NO SUBPLOTS")
    #     exec(code.code + "\nplt.savefig(file_path)\nplt.close()")

    # plt.close()
    # plt.savefig(file_path)
    # plt.close()

    # Get all open figures
    figures = [plt.figure(i) for i in plt.get_fignums()]
    
    # Save all figures in a single PDF using PdfPages
    # pdf_filename = "all_graphs.pdf"
    with PdfPages(file_path) as pdf:
        for fig in figures:
            pdf.savefig(fig)  # Save each figure as a page in the PDF
            plt.close(fig)  # Close the figure to free memory
    
    print(f"Saved all figures to {file_path}")

    
    # return Response(content=img_buffer.getvalue(), media_type="image/png")
    # return FileResponse(file_path, media_type="image/png")
    # return FileResponse(file_path, media_type="application/pdf", filename="graph.pdf")
    return {
        "message": "Graph created succesfully!",
        "url": str(request.base_url) + f"chart/{code.project_id}/{code.chain_id}/{code.session_id}"
    }



@app.get("/chart/{project_id}/{chain_id}/{session_id}")
async def get_chart(project_id: str, chain_id: str, session_id: str):
    pdf_path = f"graphs_{project_id}_{chain_id}_{session_id}.pdf"
    return FileResponse(pdf_path, media_type="application/pdf")