yasserrmd commited on
Commit
88861c9
1 Parent(s): 9455417

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -115
app.py CHANGED
@@ -1,152 +1,103 @@
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,
142
  top_p=1,
143
  stream=False,
144
  stop=None,
145
  )
146
 
147
  print(completion.choices[0].message.content or "", end="")
148
-
149
- piperFlowText=extract_md_blocks(completion.choices[0].message.content)[0]
150
- print(piperFlowText)
151
 
152
- return {"pipeFlowImage":generate_diagram(piperFlowText),"pipeFlowText":piperFlowText}
 
 
1
  from fastapi import FastAPI, File, UploadFile
2
+ from fastapi.middleware.cors import CORSMiddleware
3
  from fastapi.responses import FileResponse
4
  from groq import Groq
5
  import os
 
 
 
 
 
 
6
  from dotenv import load_dotenv
7
 
8
  load_dotenv()
9
 
10
  app = FastAPI()
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
14
 
 
 
 
 
 
 
 
 
 
 
15
 
16
  @app.get("/")
17
  def read_root():
18
  return FileResponse("index.html")
19
 
20
+ @app.post("/transcribe/")
21
+ async def transcribe_audio(file: UploadFile = File(...)):
22
  client = Groq(api_key=GROQ_API_KEY)
23
 
24
+ # Save the uploaded file temporarily
25
+ with open(file.filename, "wb") as buffer:
26
+ buffer.write(file.file.read())
27
+
28
+ # Transcribe the audio file
29
+ with open(file.filename, "rb") as audio_file:
30
+ transcription = client.audio.transcriptions.create(
31
+ file=(file.filename, audio_file.read()),
32
+ model="distil-whisper-large-v3-en",#"whisper-large-v3",
33
+ response_format="verbose_json",
34
+ )
35
+
36
+ # Remove the temporary file
37
+ os.remove(file.filename)
38
  completion = client.chat.completions.create(
39
  model="llama3-70b-8192",
40
  messages=[
41
  {
42
  "role": "system",
43
+ "content": """convert the statement paramedic or doctor to the strictly as per below json schema\n\n
44
+
45
+ {
46
+ "$schema": "http://json-schema.org/draft-07/schema#",
47
+ "title": "Triage Assessment Form",
48
+ "type": "object",
49
+ "properties": {
50
+ "voiceObservation": {
51
+ "type": "string"
52
+ },
53
+ "patientInformation": {
54
+ "type": "object",
55
+ "properties": {
56
+ "patientName": { "type": "string" },
57
+ "age": { "type": "integer" },
58
+ "gender": { "type": "string", "enum": ["male", "female", "other"] }
59
+ },
60
+ "required": ["patientName", "age", "gender"]
61
+ },
62
+ "abcdeAssessment": {
63
+ "type": "object",
64
+ "properties": {
65
+ "airwayStatus": { "type": "string", "enum": ["clear", "obstructed", "partially obstructed"] },
66
+ "breathingStatus": { "type": "string", "enum": ["normal", "labored", "not breathing"] },
67
+ "circulationStatus": { "type": "string", "enum": ["normal", "weak", "absent"] },
68
+ "disabilityStatus": { "type": "string", "enum": ["alert", "verbal", "pain", "unresponsive"] },
69
+ "exposure": { "type": "string" }
70
+ },
71
+ "required": ["airwayStatus", "breathingStatus", "circulationStatus", "disabilityStatus", "exposure"]
72
+ },
73
+ "triageClassification": {
74
+ "type": "object",
75
+ "properties": {
76
+ "triageCategory": { "type": "string", "enum": ["red", "yellow", "green", "black"] },
77
+ "comments": { "type": "string" }
78
+ },
79
+ "required": ["triageCategory"]
80
+ }
81
+ },
82
+ "required": ["patientInformation", "abcdeAssessment", "triageClassification"]
83
+ }
84
+
85
+
86
+
87
+ """
88
  },
89
  {
90
  "role": "user",
91
+ "content": "conver the paramedic statement to json " + transcription.text
92
  }
93
  ],
94
+ temperature=0.4,
95
  top_p=1,
96
  stream=False,
97
  stop=None,
98
  )
99
 
100
  print(completion.choices[0].message.content or "", end="")
 
 
 
101
 
102
+
103
+ return {"transcription": transcription.text,"details":completion.choices[0].message.content}