Spaces:
Sleeping
Sleeping
william4416
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
-
import random
|
4 |
import time
|
5 |
-
import numpy as np
|
6 |
import json
|
7 |
|
8 |
qs = {}
|
@@ -31,37 +29,31 @@ def app_inits():
|
|
31 |
for filename in filenames:
|
32 |
with open(filename, 'r') as file:
|
33 |
data = json.load(file)
|
34 |
-
qs
|
35 |
|
36 |
print("Loaded Q & A")
|
37 |
-
print("
|
38 |
-
print("CALL URL: http://127.0.0.1:7860/?dw=0.02&dl=0.0001")
|
39 |
|
40 |
return
|
41 |
|
42 |
-
with gr.Blocks(theme
|
43 |
-
chatbot = gr.Chatbot(elem_id="chatbot", layout
|
44 |
|
45 |
def get_params(request: gr.Request):
|
46 |
-
headers = request.headers
|
47 |
-
host = request.client.host
|
48 |
-
user_agent = request.headers["user-agent"]
|
49 |
params = request.query_params
|
50 |
-
|
51 |
return str(params)
|
52 |
|
53 |
with gr.Row(equal_height=True):
|
54 |
-
msg = gr.Textbox(show_label=False, placeholder="Message ChatGPT...", max_lines
|
55 |
-
btn = gr.Button(value="", min_width=80, size
|
56 |
url_params = gr.State()
|
57 |
-
|
58 |
|
59 |
demo.load(get_params, None, url_params, queue=False)
|
60 |
|
61 |
def user(user_message, history):
|
62 |
return "", history + [[user_message, None]]
|
63 |
|
64 |
-
def bot(
|
65 |
global qs
|
66 |
DelayBetweenWords = 0.1
|
67 |
DelayBetweenLetters = 0.0001
|
@@ -71,28 +63,14 @@ with gr.Blocks(theme = theme) as demo:
|
|
71 |
DelayBetweenWords = float(params.get("dw", DelayBetweenWords))
|
72 |
DelayBetweenLetters = float(params.get("dl", DelayBetweenLetters))
|
73 |
|
74 |
-
keywords = list(qs.keys())
|
75 |
-
|
76 |
text = history[-1][0]
|
77 |
-
|
78 |
-
file_location = 'images/log_data.txt'
|
79 |
-
full_path = os.path.abspath(file_location)
|
80 |
-
|
81 |
-
if text != "":
|
82 |
-
search_keyword = find_best_keyword_match(keywords, text)
|
83 |
-
else:
|
84 |
-
search_keyword = "None"
|
85 |
-
|
86 |
-
log_message("-ENTRY- [query]: " + text + " [params] " + url_params + " [keyword] " + search_keyword)
|
87 |
|
88 |
-
|
89 |
-
bot_message = "Sorry, don't know any information about this."
|
90 |
-
else:
|
91 |
-
output_text = qs.get(search_keyword, "Keyword identified. No information found.")
|
92 |
-
bot_message = output_text
|
93 |
|
94 |
-
|
95 |
-
|
|
|
|
|
96 |
|
97 |
history[-1][1] = ""
|
98 |
|
@@ -106,8 +84,6 @@ with gr.Blocks(theme = theme) as demo:
|
|
106 |
|
107 |
yield history
|
108 |
|
109 |
-
log_message("-EXIT- [query]: " + text + " [params] " + url_params + " [keyword] " + search_keyword)
|
110 |
-
|
111 |
btn.click(user, [msg, chatbot], [msg, chatbot], queue=False, show_progress=False).then(
|
112 |
bot, [chatbot, url_params], chatbot, concurrency_limit=50
|
113 |
)
|
@@ -116,57 +92,6 @@ with gr.Blocks(theme = theme) as demo:
|
|
116 |
bot, [chatbot, url_params], chatbot, concurrency_limit=50
|
117 |
)
|
118 |
|
119 |
-
def normalize(s):
|
120 |
-
"""Lowercase and strip the string to normalize it."""
|
121 |
-
return s.lower().strip()
|
122 |
-
|
123 |
-
def levenshtein_distance(s1, s2):
|
124 |
-
"""Calculate the Levenshtein distance between two strings."""
|
125 |
-
if len(s1) < len(s2):
|
126 |
-
return levenshtein_distance(s2, s1)
|
127 |
-
|
128 |
-
if len(s2) == 0:
|
129 |
-
return len(s1)
|
130 |
-
|
131 |
-
previous_row = range(len(s2) + 1)
|
132 |
-
for i, c1 in enumerate(s1):
|
133 |
-
current_row = [i + 1]
|
134 |
-
for j, c2 in enumerate(s2):
|
135 |
-
insertions = previous_row[j + 1] + 1
|
136 |
-
deletions = current_row[j] + 1
|
137 |
-
substitutions = previous_row[j] + (c1 != c2)
|
138 |
-
current_row.append(min(insertions, deletions, substitutions))
|
139 |
-
previous_row = current_row
|
140 |
-
|
141 |
-
return previous_row[-1]
|
142 |
-
|
143 |
-
def find_best_keyword_match(keywords, text, max_distance=3):
|
144 |
-
"""Find the best keyword match in the text, allowing for some misspelling."""
|
145 |
-
text_normalized = normalize(text)
|
146 |
-
best_match = None
|
147 |
-
lowest_distance = float('inf')
|
148 |
-
|
149 |
-
for keyword in keywords:
|
150 |
-
keyword_normalized = normalize(keyword)
|
151 |
-
if ' ' in keyword_normalized:
|
152 |
-
slice_length = len(keyword_normalized)
|
153 |
-
for i in range(len(text_normalized) - slice_length + 1):
|
154 |
-
text_slice = text_normalized[i:i+slice_length]
|
155 |
-
distance = levenshtein_distance(text_slice, keyword_normalized)
|
156 |
-
if distance < lowest_distance:
|
157 |
-
best_match = keyword
|
158 |
-
lowest_distance = distance
|
159 |
-
else:
|
160 |
-
for word in text_normalized.split():
|
161 |
-
distance = levenshtein_distance(word, keyword_normalized)
|
162 |
-
if distance < lowest_distance:
|
163 |
-
best_match = keyword
|
164 |
-
lowest_distance = distance
|
165 |
-
|
166 |
-
if lowest_distance <= max_distance:
|
167 |
-
return best_match
|
168 |
-
return "None"
|
169 |
-
|
170 |
from datetime import datetime
|
171 |
|
172 |
filename = 'images/log_data.txt'
|
|
|
1 |
import gradio as gr
|
2 |
import os
|
|
|
3 |
import time
|
|
|
4 |
import json
|
5 |
|
6 |
qs = {}
|
|
|
29 |
for filename in filenames:
|
30 |
with open(filename, 'r') as file:
|
31 |
data = json.load(file)
|
32 |
+
qs[filename] = data
|
33 |
|
34 |
print("Loaded Q & A")
|
35 |
+
print("JSON Files:", filenames)
|
|
|
36 |
|
37 |
return
|
38 |
|
39 |
+
with gr.Blocks(theme=theme) as demo:
|
40 |
+
chatbot = gr.Chatbot(elem_id="chatbot", layout="panel", avatar_images=("images/user.jpg", "images/bot.jpg"),)
|
41 |
|
42 |
def get_params(request: gr.Request):
|
|
|
|
|
|
|
43 |
params = request.query_params
|
|
|
44 |
return str(params)
|
45 |
|
46 |
with gr.Row(equal_height=True):
|
47 |
+
msg = gr.Textbox(show_label=False, placeholder="Message ChatGPT...", max_lines=5, container=False,)
|
48 |
+
btn = gr.Button(value="", min_width=80, size="lg", icon="images/button.jpg", scale=0)
|
49 |
url_params = gr.State()
|
|
|
50 |
|
51 |
demo.load(get_params, None, url_params, queue=False)
|
52 |
|
53 |
def user(user_message, history):
|
54 |
return "", history + [[user_message, None]]
|
55 |
|
56 |
+
def bot(history, url_params):
|
57 |
global qs
|
58 |
DelayBetweenWords = 0.1
|
59 |
DelayBetweenLetters = 0.0001
|
|
|
63 |
DelayBetweenWords = float(params.get("dw", DelayBetweenWords))
|
64 |
DelayBetweenLetters = float(params.get("dl", DelayBetweenLetters))
|
65 |
|
|
|
|
|
66 |
text = history[-1][0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
+
bot_message = "Sorry, I couldn't find an answer to your question."
|
|
|
|
|
|
|
|
|
69 |
|
70 |
+
for filename, data in qs.items():
|
71 |
+
if text in data:
|
72 |
+
bot_message = data[text]
|
73 |
+
break
|
74 |
|
75 |
history[-1][1] = ""
|
76 |
|
|
|
84 |
|
85 |
yield history
|
86 |
|
|
|
|
|
87 |
btn.click(user, [msg, chatbot], [msg, chatbot], queue=False, show_progress=False).then(
|
88 |
bot, [chatbot, url_params], chatbot, concurrency_limit=50
|
89 |
)
|
|
|
92 |
bot, [chatbot, url_params], chatbot, concurrency_limit=50
|
93 |
)
|
94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
from datetime import datetime
|
96 |
|
97 |
filename = 'images/log_data.txt'
|