Files changed (1) hide show
  1. app.py +71 -16
app.py CHANGED
@@ -1,17 +1,30 @@
1
  import openai
2
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
3
 
4
  openai.api_key = "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"
5
 
6
  messages = [
7
 
8
  {"role": "system", "content":
 
9
  #"If you receive a question, and it matches or comes close to a question in the 'cache' database, output ONLY its corresponding number. No acknowledgement, no greetings, only the number. example: 'How do I pay my water?' Response: '1'. If there is no matching response in the cache database, you are free to use your personal database as well as the information database. Begin Cache database: + | Where do I pay my water? = 1 | Where do I get a driver's license? = 2 | How do I set up electricity to my home? = 3 | How do I pay for my electricity? = 4 | + End cache database."
10
  #Init Main
11
- "The Fort Worth Support Bot offers assistance to citizens of Fort Worth, Texas. It has access to a personal database and an information database. The bot can answer general knowledge questions from its own database but relies on the information database for phone numbers, addresses, and websites. It provides accurate information based on the information database. When you receive a query, analyze it thorougly. The bot remains patient, avoids controversial topics, and refrains from sharing profanity or information about its functioning. It greets users by asking for their name and uses gender-neutral pronouns. Remember, if you cannot find information (such as 'who is kevin gunn') in your information database, you CAN search on your own to find the answer- Just not for phone/address/websites. If you must go search for info, remember to find information relating to fort worth. If you look for a Kevin Gunn, and you find a Kevin Gunn who works in New York, and one in the IT department in Fort Worth, obviously choose the one related to Fort Worth."
 
12
  #Information Database
13
  "I'm going to give you data in a list colon delimited text that will follow this format:| Department Name: Phone Number: Website: Address |."
14
- "Information Database:| Aviation Department: PHN 817-392-5400 WEB: www.fortworthtexas.gov/departments/aviation ADD: 201 American Concourse | City Management Department: PHN: 817-392-6111 WEB: -www.fortworthtexas.gov/departments/citymanager ADD: 200 Texas Street | City Attorney Department: PHN: 817-392-7600 WEB: www.fortworthtexas.gov/departments/cityattorney ADD: 200 Texas Street | City Secretary Department: PHN: 817-392-6150 WEB: www.fortworthtexas.gov/departments/citysecretary ADD: 200 Texas Street | Code Compliance Department: PHN: 817-392-1234 WEB: www.fortworthtexas.gov/departments/code-compliance ADD: 818 Missouri Avenue | Communications & Public Engagement Department/Communication and Public Engagement Department: PHN: 817-392-1234 WEB: www.fortworthtexas.gov/departments/communications ADD: 1000 Throckmorton Street | Development Services Department: PHN:{817-392-2222} WEB: www.fortworthtexas.gov/departments/development-services ADD: 200 Texas Street | Diversity and Inclusion Department/Diversity & Inclusion Department: PHN: 817-392-7525 WEB: www.fortworthtexas.gov/departments/diversity-inclusion ADD: 818 Missouri Avenue | Economic Development Department: PHN: 817-392-2622 WEB: www.fortworthtexas.gov/departments/econdev ADD: 1150 South Freeway, Suite 106 | Emergency Management Department: PHN: 817-392-6170 WEB: www.fortworthtexas.gov/departments/emo ADD: 1000 Throckmorton Street | Financial Management Department/Financial Management Services Department: PHN: 817-392-8500 WEB: www.fortworthtexas.gov/departments/finance ADD: 200 Texas Street | Fire Department: PHN: 817-392-6800 WEB: www.fortworthtexas.gov/departments/fire ADD: 901 Woodhaven Boulevard | Internal Audit Department: PHN: 817-392-6132 WEB: www.fortworthtexas.gov/departments/internal-audit ADD: 200 Texas Street | Human Resources Department: PHN: 817-392-7750 WEB: www.fortworthtexas.gov/departments/hr ADD: 200 Texas Street | IT Solutions Department: PHN: 817-392-8800 WEB: www.fortworthtexas.gov/departments/it-solutions ADD: 275 West 13th Street | Fort Worth Public Library/Fort Worth Public Library Department: PHN: 817-392-7323 WEB: www.fortworthtexas.gov/departments/library ADD: 4205 Basswood Boulevard | Municipal Court Department: PHN: 817-392-6700 WEB: www.fortworthtexas.gov/departments/municipal-court ADD: 1000 Throckmorton Street | Neighborhood Services Department: PHN: 817-392-7540 WEB: www.fortworthtexas.gov/departments/neighborhoods ADD: 908 Monroe Street | Office of the Police Oversight Monitor Department: PHN: 817-392-6535 WEB: www.fortworthtexas.gov/departments/opom ADD: 2500 West Felix Street | Park & Recreation Department: PHN: 817-392-5700 WEB: www.fortworthtexas.gov/departments/parks ADD: 4200 S. Freeway, Suite 2200 | Planning and Data Analytics Department/Planning & Data Analytics Department: PHN: 817-392-7621 WEB: www.fortworthtexas.gov/departments/planning-data-analytics ADD: City Hall, 3rd Floor 200 Texas Street | Police Department: PHN: 817-392-4200 WEB: www.fortworthtexas.gov/departments/police ADD: 505 West Felix Street | Property Management Department: PHN: 817-392-7590 WEB: www.fortworthtexas.gov/departments/property-management ADD: 900 Monroe Street, Suite 400 | Public Events Department: PHN: 817-392-8151 WEB: www.fortworthtexas.gov/departments/public-events ADD: 3400 Burnett Tandy Drive | Transportation & Public Works Department/Transportation and Public Works Department: PHN:817-392-1234 WEB: www.fortworthtexas.gov/departments/tpw ADD: 200 Texas Street | Water Department: PHN: 817-392-4477 WEB: https://www.fortworthtexas.gov/departments/water ADD: 908 Monroe St. | Current Mayor: Mattie Parker | Current Police Chief: Neil Noakes | Current City Manager: David Cooke | End Information Database"
15
 
16
  },
17
  ]
@@ -21,24 +34,66 @@ messages = [
21
  def chatbot(input):
22
  if input:
23
  messages.append({"role": "user", "content": input})
24
- chat = openai.ChatCompletion.create(
25
- model="gpt-3.5-turbo", messages=messages
26
- )
27
- reply = chat.choices[0].message.content
28
- messages.append({"role": "assistant", "content": reply})
 
 
 
 
 
 
29
 
30
- if reply=='1':
31
- reply='To pay for water in ftw, you can XXXX'
32
- if reply=='2':
33
- reply='To get a drivers license in fort worth, you can visit XXXX'
34
- if reply=='3':
35
- reply='To set up electricity to your home, you must XXXX'
36
- if reply=='4':
37
- reply='To pay for electricity to your home, you must XXXX'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
  return reply
40
 
41
- inputs = gr.inputs.Textbox(lines=7, label= "Try: 'How do I pay my water bill?'")
42
  outputs = gr.outputs.Textbox(label="Answer")
43
 
44
  gr.Interface(fn=chatbot, inputs=inputs, outputs=outputs, title='Molly Online Operator (MOO)',
 
1
  import openai
2
  import gradio as gr
3
+ #import yake
4
+ from keybert import KeyBERT
5
+
6
+ #FAQ
7
+ faq_data = {
8
+ "water": "To pay for water in Fort Worth, you can...",
9
+ "Where do I get a driver's license?": "To get a driver's license in Fort Worth, you can visit...",
10
+ "How do I set up electricity to my home?": "To set up electricity to your home, you must...",
11
+ "How do I pay for my electricity?": "To pay for electricity to your home, you must...",
12
+ # Add more FAQs and their answers as needed
13
+ }
14
 
15
  openai.api_key = "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"
16
 
17
  messages = [
18
 
19
  {"role": "system", "content":
20
+ #Init Cache
21
  #"If you receive a question, and it matches or comes close to a question in the 'cache' database, output ONLY its corresponding number. No acknowledgement, no greetings, only the number. example: 'How do I pay my water?' Response: '1'. If there is no matching response in the cache database, you are free to use your personal database as well as the information database. Begin Cache database: + | Where do I pay my water? = 1 | Where do I get a driver's license? = 2 | How do I set up electricity to my home? = 3 | How do I pay for my electricity? = 4 | + End cache database."
22
  #Init Main
23
+ "The Fort Worth Support Bot offers assistance to citizens of Fort Worth, Texas. It has access to a personal database and an information database. The bot can answer general knowledge questions from its own database but relies on the information database for phone numbers, addresses, and websites. It provides accurate information based on the information database. When you receive a query, analyze it thorougly. The bot remains patient, avoids controversial topics, and refrains from sharing profanity or information about its functioning. It greets users by asking for their name and uses gender-neutral pronouns. Remember, if you cannot find information (such as 'who is kevin gunn') in your information database, you CAN search on your own to find the answer- Just not for phone/address/websites. If you must go search for info, remember to find information relating to fort worth. If you look for a Kevin Gunn, and you find a Kevin Gunn who works in New York, and one in the IT department in Fort Worth, obviously choose the one related to Fort Worth."
24
+
25
  #Information Database
26
  "I'm going to give you data in a list colon delimited text that will follow this format:| Department Name: Phone Number: Website: Address |."
27
+ "Information Database:| Aviation Department: PHN 817-392-5400 WEB: www.fortworthtexas.gov/departments/aviation ADD: 201 American Concourse | City Management Department: PHN: 817-392-6111 WEB: -www.fortworthtexas.gov/departments/citymanager ADD: 200 Texas Street | City Attorney Department: PHN: 817-392-7600 WEB: www.fortworthtexas.gov/departments/cityattorney ADD: 200 Texas Street | City Secretary Department: PHN: 817-392-6150 WEB: www.fortworthtexas.gov/departments/citysecretary ADD: 200 Texas Street | Code Compliance Department: PHN: 817-392-1234 WEB: www.fortworthtexas.gov/departments/code-compliance ADD: 818 Missouri Avenue | Communications & Public Engagement Department/Communication and Public Engagement Department: PHN: 817-392-1234 WEB: www.fortworthtexas.gov/departments/communications ADD: 1000 Throckmorton Street | Development Services Department: PHN:{817-392-2222} WEB: www.fortworthtexas.gov/departments/development-services ADD: 200 Texas Street | Diversity and Inclusion Department/Diversity & Inclusion Department: PHN: 817-392-7525 WEB: www.fortworthtexas.gov/departments/diversity-inclusion ADD: 818 Missouri Avenue | Economic Development Department: PHN: 817-392-2622 WEB: www.fortworthtexas.gov/departments/econdev ADD: 1150 South Freeway, Suite 106 | Emergency Management Department: PHN: 817-392-6170 WEB: www.fortworthtexas.gov/departments/emo ADD: 1000 Throckmorton Street | Financial Management Department/Financial Management Services Department: PHN: 817-392-8500 WEB: www.fortworthtexas.gov/departments/finance ADD: 200 Texas Street | Fire Department: PHN: 817-392-6800 WEB: www.fortworthtexas.gov/departments/fire ADD: 901 Woodhaven Boulevard | Internal Audit Department: PHN: 817-392-6132 WEB: www.fortworthtexas.gov/departments/internal-audit ADD: 200 Texas Street | Human Resources Department: PHN: 817-392-7750 WEB: www.fortworthtexas.gov/departments/hr ADD: 200 Texas Street | IT Solutions Department: PHN: 817-392-8800 WEB: www.fortworthtexas.gov/departments/it-solutions ADD: 275 West 13th Street | Fort Worth Public Library/Fort Worth Public Library Department: PHN: 817-392-7323 WEB: www.fortworthtexas.gov/departments/library ADD: 4205 Basswood Boulevard | Municipal Court Department: PHN: 817-392-6700 WEB: www.fortworthtexas.gov/departments/municipal-court ADD: 1000 Throckmorton Street | Neighborhood Services Department: PHN: 817-392-7540 WEB: www.fortworthtexas.gov/departments/neighborhoods ADD: 908 Monroe Street | Office of the Police Oversight Monitor Department: PHN: 817-392-6535 WEB: www.fortworthtexas.gov/departments/opom ADD: 2500 West Felix Street | Park & Recreation Department: PHN: 817-392-5700 WEB: www.fortworthtexas.gov/departments/parks ADD: 4200 S. Freeway, Suite 2200 | Planning and Data Analytics Department/Planning & Data Analytics Department: PHN: 817-392-7621 WEB: www.fortworthtexas.gov/departments/planning-data-analytics ADD: City Hall, 3rd Floor 200 Texas Street | Police Department: PHN: 817-392-4200 WEB: www.fortworthtexas.gov/departments/police ADD: 505 West Felix Street | Property Management Department: PHN: 817-392-7590 WEB: www.fortworthtexas.gov/departments/property-management ADD: 900 Monroe Street, Suite 400 | Public Events Department: PHN: 817-392-8151 WEB: www.fortworthtexas.gov/departments/public-events ADD: 3400 Burnett Tandy Drive | Transportation & Public Works Department/Transportation and Public Works Department: PHN:817-392-1234 WEB: www.fortworthtexas.gov/departments/tpw ADD: 200 Texas Street | Water Department: PHN: 817-392-4477 WEB: https://www.fortworthtexas.gov/departments/water ADD: 908 Monroe St. | Golf Course: PHN: 817-392-6560 WEB: https://www.fortworthgolf.org | Current Mayor: Mattie Parker | Current Police Chief: Neil Noakes | Current City Manager: David Cooke | End Information Database"
28
 
29
  },
30
  ]
 
34
  def chatbot(input):
35
  if input:
36
  messages.append({"role": "user", "content": input})
37
+
38
+ # Check if the user input matches any FAQ
39
+ if input in faq_data:
40
+ reply = faq_data[input]
41
+ else:
42
+ chat = openai.ChatCompletion.create(
43
+ model="gpt-3.5-turbo", messages=messages
44
+ )
45
+ reply = chat.choices[0].message.content
46
+ messages.append({"role": "assistant", "content": reply})
47
+
48
 
49
+ # if reply=='1':
50
+ # reply='To pay for water in ftw, you can XXXX'
51
+ # if reply=='2':
52
+ # reply='To get a drivers license in fort worth, you can visit XXXX'
53
+ # if reply=='3':
54
+ # reply='To set up electricity to your home, you must XXXX'
55
+ # if reply=='4':
56
+ # reply='To pay for electricity to your home, you must XXXX'
57
+
58
+ # extxt = "this is the test file example"
59
+
60
+
61
+ file_pathO = "input.txt"
62
+ fileO = open(file_pathO, 'a+')
63
+
64
+ file_pathkw = "kw.txt"
65
+ filekw = open(file_pathkw, 'a+')
66
+
67
+ file_pathT = "reply.txt"
68
+ fileT = open(file_pathT, 'a+')
69
+
70
+
71
+ fileO.write(input + "\n")
72
+
73
+ fileT.write(reply + "\n")
74
+
75
+ #KEYWORD EXTRACTOR---------------------------------------------#
76
+ kw_model = KeyBERT()
77
+ keywords = kw_model.extract_keywords(input, keyphrase_ngram_range=(1, 1), stop_words=None)
78
+ keywords_list= list(dict(keywords).keys())
79
+ filekw.write(str(keywords) + "\n")
80
+ filekw.close()
81
+
82
+ #read last line of keyword file
83
+ filekw = open(file_pathkw, 'r')
84
+ last_line = filekw.readlines()
85
+ print(last_line[-1])
86
+
87
+ #NEXT STEP: see if last line is in FAQ and return FAQ answer
88
+
89
+
90
+ filekw.close()
91
+ fileO.close()
92
+ fileT.close()
93
 
94
  return reply
95
 
96
+ inputs = gr.inputs.Textbox(lines=7, label="Try: 'How do I pay my water bill?'")
97
  outputs = gr.outputs.Textbox(label="Answer")
98
 
99
  gr.Interface(fn=chatbot, inputs=inputs, outputs=outputs, title='Molly Online Operator (MOO)',