Pepsr commited on
Commit
534c0fb
·
1 Parent(s): fb4a203

add application file

Browse files
Files changed (1) hide show
  1. app.py +60 -0
app.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from getFlightData import getFlightInfo
3
+ from langchain.agents import initialize_agent, Tool, load_tools
4
+ from langchain.chat_models import ChatOpenAI
5
+ from datetime import datetime
6
+ import gradio as gr
7
+ from langchain.memory import ConversationBufferMemory
8
+
9
+ os.environ["OPENAI_API_KEY"] = "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"
10
+
11
+
12
+ today = datetime.today().strftime("%Y-%m-%d")
13
+
14
+ memory = ConversationBufferMemory(memory_key="chat_history")
15
+ llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0)
16
+
17
+ def parsing_info(string):
18
+ flight_n, date = string.split(",")
19
+ if flight_n == "flight_number":
20
+ return "Please provide the flight number"
21
+ elif date == "date":
22
+ return "Please provide the date"
23
+ else:
24
+ date = datetime.strptime(date.strip(), "%Y-%m-%d").date()
25
+ info = getFlightInfo(flight_n, date)
26
+ return info
27
+
28
+ def getDate(string):
29
+ today = datetime.today().strftime("%Y-%m-%d")
30
+ return today
31
+
32
+
33
+
34
+ custom_tools = [
35
+ Tool(
36
+ name = "getSpecificInfoFlight",
37
+ func=parsing_info,
38
+ description= f"Useful for when the user wants to know specific information about a flight ( departure time, gate, etc.). Remember that today's date is {today}. The action input has to be in the following format: 'flight_number, Flight_date'.",
39
+ ),
40
+ ]
41
+
42
+ tools = custom_tools
43
+
44
+
45
+ agent = initialize_agent(tools, llm, agent="conversational-react-description", verbose=True, memory=memory)
46
+
47
+
48
+
49
+
50
+ def main(user_input, history=[]):
51
+ answer = agent.run(user_input)
52
+ history.append((user_input, answer))
53
+
54
+ return history, history
55
+
56
+ inputs = gr.components.Textbox(lines=7, label="Chat with AI")
57
+ outputs = gr.components.Textbox(label="Reply")
58
+
59
+ gr.Interface(fn=main, inputs=["text","state"], outputs=["chatbot","state"], title="AI Chatbot",
60
+ theme="compact").launch(share=True, debug=True)