yasserrmd commited on
Commit
ed7adeb
1 Parent(s): 26063ff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +155 -155
app.py CHANGED
@@ -1,156 +1,156 @@
1
- from fastapi import FastAPI, File, UploadFile
2
- from fastapi.responses import FileResponse
3
- from groq import Groq
4
- import os
5
- import base64
6
- from io import BytesIO
7
- from PIL import Image
8
- from mdextractor import extract_md_blocks
9
- from pydantic import BaseModel
10
- from processpiper.text2diagram import render
11
- from dotenv import load_dotenv
12
-
13
- load_dotenv()
14
-
15
- app = FastAPI()
16
-
17
- PIPERFLOW_SYNTAX_DOC=""" Generate BPMN diagram using English like PiperFlow syntax
18
- To create a process map using PirperFlow, you need to define the diagram using a specific syntax. Here is an example:
19
-
20
-
21
-
22
-
23
- title: Sample Test Process
24
- colourtheme: GREENTURTLE
25
- lane: End User
26
- (start) as start
27
- [Enter Keyword] as enter_keyword
28
- (end) as end
29
- pool: System Search
30
- lane: Database System
31
- [Login] as login
32
- [Search Records] as search_records
33
- <Result Found?> as result_found
34
- [Display Result] as display_result
35
- [Logout] as logout
36
- lane: Log System
37
- [Log Error] as log_error
38
-
39
- start->login->enter_keyword->search_records->result_found->display_result->logout->end
40
- result_found->log_error->display_result
41
-
42
- footer: Generated by ProcessPiper
43
-
44
-
45
-
46
- Import the render function from processpiper.text2diagram
47
- Call the render function with the input syntax and the output file name.
48
- Syntax
49
- Diagram Configurations
50
- The PiperFlow syntax for defining a process map is as follows:
51
-
52
- title: The title of the diagram.
53
- footer: The footer text to display on the diagram.
54
- width : (Optional) Specify the width of the diagram.
55
- colourtheme: The colour theme to use
56
- Lane & Pool Configurations
57
- lane: The name of the lane.
58
- pool: The name of the pool.
59
- Element/Shape Configurations
60
- To add elements to the lane, use one of the following tags. You place your element description within the tag:
61
- Use ( and ) to create event element
62
- use (start) to create a start event
63
- use (end) to create an end event
64
- use (@timer and ) to create a timer event. Example (@timer Trigger every 1 hour) as timer_event
65
- use (@intermediate and ) to create an intermediate event. Example (@intermediate Message Received) as intermediate_event
66
- use (@message and ) to create a message event
67
- use (@signal and ) to create a signal event
68
- use (@conditional and ) to create a conditional event
69
- use (@link and ) to create a link event
70
- Use [ and ] to create an activity. By default, the activity type is TASK. Example [Place Order] as place_order
71
- use [@subprocess] to create a subprocess. Example `[@subprocess Get Approval] as get_approval``
72
- Use < and > to create a gateway. By default, the gateway type is EXCLUSIVE. Example <Result Found?> as result_found
73
- Use <@parallel and > to create a parallel gateway. Example <@parallel Span Out> as span_out
74
- Use <@inclusive and > to create an inclusive gateway. Example <@inclusive Condition Met?> as condition_met
75
- Use <@event and > to create an event gateway
76
- Connection Configurations
77
- To connect two elements, use ->. You can chain multiple connections using ->:
78
- Example:
79
- login->enter_keyword
80
- start->login->enter_keyword->search_records->result_found->display_result->logout->end
81
- To add label to the connection, add ":" when connecting elements. ❗ NOTE: This is a breaking change in v0.6. Versions prior to 0.6 use start-"Enter Credentials->login syntax. See this page for more information.
82
- Example:
83
- start->login: Enter credentials
84
- To specify the connection point manually, add connection side. See How to select sides for more information.
85
- Example:
86
- start-(bottom, top)->login
87
- start-(bottom, top)->login: Enter credentials
88
- Indentation is not required. However, it is recommended to use indentation to make the diagram easier to read.
89
-
90
- currently available color themes are
91
- Default
92
- GREYWOOF
93
- BLUEMOUNTAIN
94
- ORANGEPEEL
95
- GREENTURTLE
96
- SUNFLOWER
97
- PURPLERAIN
98
- RUBYRED
99
- TEALWATERS
100
- SEAFOAMS
101
-
102
- """
103
-
104
-
105
-
106
-
107
- GROQ_API_KEY = os.getenv("GROQ_API_KEY")
108
-
109
- class Prompt(BaseModel):
110
- prompt: str
111
-
112
- def generate_diagram(input_syntax):
113
- _, generated_image = render(input_syntax)
114
- # Convert the image to a base64 encoded string
115
- buffered = BytesIO()
116
- generated_image.save(buffered, format="PNG")
117
- img_str = base64.b64encode(buffered.getvalue())
118
- return img_str.decode("utf-8")
119
-
120
- @app.get("/")
121
- def read_root():
122
- return FileResponse("index.html")
123
-
124
- @app.post("/generate/")
125
- async def generate_bpmn(prompt:Prompt):
126
- client = Groq(api_key=GROQ_API_KEY)
127
-
128
-
129
- completion = client.chat.completions.create(
130
- model="llama3-70b-8192",
131
- messages=[
132
- {
133
- "role": "system",
134
- "content": "you are business process flow generator using the following piperflow text\n\n" + PIPERFLOW_SYNTAX_DOC
135
- },
136
- {
137
- "role": "user",
138
- "content": "generate the piperflow text for the below scenario\n\n" + prompt.prompt
139
- }
140
- ],
141
- temperature=1,
142
- max_tokens=1024,
143
- top_p=1,
144
- stream=False,
145
- stop=None,
146
- )
147
-
148
- print(completion.choices[0].message.content or "", end="")
149
-
150
- piperFlowText=extract_md_blocks(completion.choices[0].message.content)[0]
151
- print(piperFlowText)
152
-
153
- return {"pipeFlowImage":generate_diagram(piperFlowText),"pipeFlowText":piperFlowText}
154
- if __name__ == "__main__":
155
- import uvicorn
156
  uvicorn.run(app, host="0.0.0.0", port=8000)
 
1
+ from fastapi import FastAPI, File, UploadFile
2
+ from fastapi.responses import FileResponse
3
+ from groq import Groq
4
+ import os
5
+ import base64
6
+ from io import BytesIO
7
+ from PIL import Image
8
+ from mdextractor import extract_md_blocks
9
+ from pydantic import BaseModel
10
+ from processpiper.text2diagram import render
11
+ from dotenv import load_dotenv
12
+
13
+ load_dotenv()
14
+
15
+ app = FastAPI()
16
+
17
+ PIPERFLOW_SYNTAX_DOC=""" Generate BPMN diagram using English like PiperFlow syntax
18
+ To create a process map using PirperFlow, you need to define the diagram using a specific syntax. Here is an example:
19
+
20
+
21
+
22
+
23
+ title: Sample Test Process
24
+ colourtheme: GREENTURTLE
25
+ lane: End User
26
+ (start) as start
27
+ [Enter Keyword] as enter_keyword
28
+ (end) as end
29
+ pool: System Search
30
+ lane: Database System
31
+ [Login] as login
32
+ [Search Records] as search_records
33
+ <Result Found?> as result_found
34
+ [Display Result] as display_result
35
+ [Logout] as logout
36
+ lane: Log System
37
+ [Log Error] as log_error
38
+
39
+ start->login->enter_keyword->search_records->result_found->display_result->logout->end
40
+ result_found->log_error->display_result
41
+
42
+ footer: Generated by ProcessPiper
43
+
44
+
45
+
46
+ Import the render function from processpiper.text2diagram
47
+ Call the render function with the input syntax and the output file name.
48
+ Syntax
49
+ Diagram Configurations
50
+ The PiperFlow syntax for defining a process map is as follows:
51
+
52
+ title: The title of the diagram.
53
+ footer: The footer text to display on the diagram.
54
+ width : (Optional) Specify the width of the diagram.
55
+ colourtheme: The colour theme to use
56
+ Lane & Pool Configurations
57
+ lane: The name of the lane.
58
+ pool: The name of the pool.
59
+ Element/Shape Configurations
60
+ To add elements to the lane, use one of the following tags. You place your element description within the tag:
61
+ Use ( and ) to create event element
62
+ use (start) to create a start event
63
+ use (end) to create an end event
64
+ use (@timer and ) to create a timer event. Example (@timer Trigger every 1 hour) as timer_event
65
+ use (@intermediate and ) to create an intermediate event. Example (@intermediate Message Received) as intermediate_event
66
+ use (@message and ) to create a message event
67
+ use (@signal and ) to create a signal event
68
+ use (@conditional and ) to create a conditional event
69
+ use (@link and ) to create a link event
70
+ Use [ and ] to create an activity. By default, the activity type is TASK. Example [Place Order] as place_order
71
+ use [@subprocess] to create a subprocess. Example `[@subprocess Get Approval] as get_approval``
72
+ Use < and > to create a gateway. By default, the gateway type is EXCLUSIVE. Example <Result Found?> as result_found
73
+ Use <@parallel and > to create a parallel gateway. Example <@parallel Span Out> as span_out
74
+ Use <@inclusive and > to create an inclusive gateway. Example <@inclusive Condition Met?> as condition_met
75
+ Use <@event and > to create an event gateway
76
+ Connection Configurations
77
+ To connect two elements, use ->. You can chain multiple connections using ->:
78
+ Example:
79
+ login->enter_keyword
80
+ start->login->enter_keyword->search_records->result_found->display_result->logout->end
81
+ To add label to the connection, add ":" when connecting elements. ❗ NOTE: This is a breaking change in v0.6. Versions prior to 0.6 use start-"Enter Credentials->login syntax. See this page for more information.
82
+ Example:
83
+ start->login: Enter credentials
84
+ To specify the connection point manually, add connection side. See How to select sides for more information.
85
+ Example:
86
+ start-(bottom, top)->login
87
+ start-(bottom, top)->login: Enter credentials
88
+ Indentation is not required. However, it is recommended to use indentation to make the diagram easier to read.
89
+
90
+ currently available color themes are
91
+ Default
92
+ GREYWOOF
93
+ BLUEMOUNTAIN
94
+ ORANGEPEEL
95
+ GREENTURTLE
96
+ SUNFLOWER
97
+ PURPLERAIN
98
+ RUBYRED
99
+ TEALWATERS
100
+ SEAFOAMS
101
+
102
+ """
103
+
104
+
105
+
106
+
107
+ GROQ_API_KEY = os.getenv("GROQ_API_KEY")
108
+
109
+ class Prompt(BaseModel):
110
+ prompt: str
111
+
112
+ def generate_diagram(input_syntax):
113
+ _, generated_image = render(input_syntax)
114
+ # Convert the image to a base64 encoded string
115
+ buffered = BytesIO()
116
+ generated_image.save(buffered, format="PNG")
117
+ img_str = base64.b64encode(buffered.getvalue())
118
+ return img_str.decode("utf-8")
119
+
120
+ @app.get("/")
121
+ def read_root():
122
+ return FileResponse("index.html")
123
+
124
+ @app.post("/generate/")
125
+ async def generate_bpmn(prompt:Prompt):
126
+ client = Groq(api_key=GROQ_API_KEY)
127
+
128
+
129
+ completion = client.chat.completions.create(
130
+ model="llama3-70b-8192",
131
+ messages=[
132
+ {
133
+ "role": "system",
134
+ "content": "you are business process flow generator using the following piperflow text\n\n" + PIPERFLOW_SYNTAX_DOC
135
+ },
136
+ {
137
+ "role": "user",
138
+ "content": "generate the piperflow text for the below scenario\n\n" + prompt.prompt
139
+ }
140
+ ],
141
+ temperature=0.2,
142
+ max_tokens=1024,
143
+ top_p=1,
144
+ stream=False,
145
+ stop=None,
146
+ )
147
+
148
+ print(completion.choices[0].message.content or "", end="")
149
+
150
+ piperFlowText=extract_md_blocks(completion.choices[0].message.content)[0]
151
+ print(piperFlowText)
152
+
153
+ return {"pipeFlowImage":generate_diagram(piperFlowText),"pipeFlowText":piperFlowText}
154
+ if __name__ == "__main__":
155
+ import uvicorn
156
  uvicorn.run(app, host="0.0.0.0", port=8000)