Delete LLM_automation_GPT35.py
Browse files- LLM_automation_GPT35.py +0 -87
LLM_automation_GPT35.py
DELETED
@@ -1,87 +0,0 @@
|
|
1 |
-
def create_data(description):
|
2 |
-
from langchain_core.prompts import ChatPromptTemplate ### To create a chatbot, chatprompttemplate used
|
3 |
-
from langchain_openai import ChatOpenAI ##### For using chat openai features
|
4 |
-
from langchain_core.output_parsers import StrOutputParser ### Default output parser. Custom parser can also be created
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
import os
|
9 |
-
from dotenv import load_dotenv
|
10 |
-
|
11 |
-
|
12 |
-
load_dotenv()
|
13 |
-
|
14 |
-
### Set all api keys:
|
15 |
-
os.environ["OPENAI_API_KEY"]=os.getenv('OPENAI_API')
|
16 |
-
|
17 |
-
|
18 |
-
### Create Prompt Template:
|
19 |
-
prompt=ChatPromptTemplate.from_messages(
|
20 |
-
{
|
21 |
-
("system", "You are a helpful assistant, please respond to the queries"), ### We need both system and users in prompt
|
22 |
-
("user","question: {question}")
|
23 |
-
}
|
24 |
-
)
|
25 |
-
df2=description
|
26 |
-
#### Create OpenAI llm:
|
27 |
-
llm=ChatOpenAI(model="gpt-3.5-turbo")
|
28 |
-
|
29 |
-
### Create an output parser:
|
30 |
-
output_parser=StrOutputParser()
|
31 |
-
|
32 |
-
#### Creating chain: The concept is- output of action before | symbol will be passed as input in action after the symbol.
|
33 |
-
#### Here we have created three actions: The prompt, llm and output parser:
|
34 |
-
chain=prompt|llm|output_parser
|
35 |
-
|
36 |
-
dj=[]
|
37 |
-
for i in range(len(df2)):
|
38 |
-
dj.append(chain.invoke({"question" : df2['Description'][i]+" Is the news about road accident? If no, then reply 'General'. Else if the news is about road accident then check if the news is referring to a specific accident incident or accident in general? Answer only in a word: Either specific or general."}))
|
39 |
-
|
40 |
-
df2['Report Type']=dj
|
41 |
-
|
42 |
-
def drp(p):
|
43 |
-
df2.drop([p],inplace=True)
|
44 |
-
### Removing the general accident types:
|
45 |
-
for p in range(len(df2)):
|
46 |
-
if "General" in df2['Report Type'][p] or "general" in df2['Report Type'][p]:
|
47 |
-
drp(p)
|
48 |
-
|
49 |
-
### Reseting index of df3:
|
50 |
-
df2.reset_index(drop=True,inplace=True)
|
51 |
-
|
52 |
-
|
53 |
-
### Splitting dj2 string based on comma position:
|
54 |
-
Date=[]
|
55 |
-
Time=[]
|
56 |
-
Killed=[]
|
57 |
-
Injured=[]
|
58 |
-
Location=[]
|
59 |
-
Road_Characteristic=[]
|
60 |
-
Pedestrian_Involved=[]
|
61 |
-
vehicles=[]
|
62 |
-
#Weather=[]
|
63 |
-
|
64 |
-
for i in range(len(df2)):
|
65 |
-
Date.append(chain.invoke({"question" : "Read the accident report carefully and provide only the answer of the question asked. Do not add any extra sentences or words except the answer: What is the date of accident occurrence in Day-Month-Year format. Keep in mind that news publish date and accident occurrence date may be different. If you cannot find or deduce the answer, simply reply Not Available" + df2['Description'][i]}))
|
66 |
-
Time.append(chain.invoke({"question" : "Read the accident report carefully and provide only the answer of the question asked. Do not add any extra sentences or words except the answer: What is the time of accident occurrence in 24-hour format. If you cannot find or deduce the answer, simply reply Not Available" + df2['Description'][i]}))
|
67 |
-
Killed.append(chain.invoke({"question" : "Read the accident report carefully and provide only the answer of the question asked. Do not add any extra sentences or words except the answer: How many people were killed in the accident?. If you cannot find or deduce the answer, simply reply Not Available" + df2['Description'][i]}))
|
68 |
-
time.sleep(30)
|
69 |
-
Injured.append(chain.invoke({"question" : "Read the accident report carefully and provide only the answer of the question asked. Do not add any extra sentences or words except the answer: How many people were injured in the accident?. If you cannot find or deduce the answer, simply reply Not Available" + df2['Description'][i]}))
|
70 |
-
Location.append(chain.invoke({"question" : "Read the accident report carefully and provide only the answer of the question asked. Do not add any extra sentences or words except the answer: What is the name of the location where accident took place?. If you cannot find or deduce the answer, simply reply Not Available" + df2['Description'][i]}))
|
71 |
-
Road_Characteristic.append(chain.invoke({"question" : "Read the accident report carefully and provide only the answer of the question asked. Do not add any extra sentences or words except the answer: What is the type of road where accident took place?. If you cannot find or deduce the answer, simply reply Not Available" + df2['Description'][i]}))
|
72 |
-
time.sleep(30)
|
73 |
-
Pedestrian_Involved.append(chain.invoke({"question" : "Read the accident report carefully and provide only the answer of the question asked. Do not add any extra sentences or words except the answer: Was there any pedestrian involved in the accident?. If you cannot find or deduce the answer, simply reply Not Available" + df2['Description'][i]}))
|
74 |
-
vehicles.append(chain.invoke({"question" : "Only name the type of vehicles involved in the accident. If multiple vehicles are involved, seperate them by hyphens(-). Example answers: Bus, Truck-Bus etc. If no vehicles are mentioned, your answer will be: Not Available. Your answer should only contain the vehicle name, do not include any extra sentences" + df2['Description'][i]}))
|
75 |
-
time.sleep(30)
|
76 |
-
|
77 |
-
#### Probable type of final dataframe:
|
78 |
-
df2["Date"]=Date
|
79 |
-
df2["Time"]=Time
|
80 |
-
df2["Killed"]=Killed
|
81 |
-
df2["Injured"]=Injured
|
82 |
-
df2["Location"]=Location
|
83 |
-
df2["Road_Characteristic"]=Road_Characteristic
|
84 |
-
df2["Pedestrian_Involved"]=Pedestrian_Involved
|
85 |
-
df2["Vehicles Involved"]=vehicles
|
86 |
-
df3=df2.drop(columns=['Description','Report Type','Date + Desc'])
|
87 |
-
return df3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|