Spaces:
Sleeping
Sleeping
william4416
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,65 +1,183 @@
|
|
1 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
2 |
import gradio as gr
|
3 |
-
import
|
|
|
|
|
|
|
4 |
import json
|
5 |
|
|
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
|
|
10 |
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
)
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
)
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
if
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 = {}
|
9 |
|
10 |
+
theme = gr.themes.Base(
|
11 |
+
primary_hue="gray",
|
12 |
+
secondary_hue="gray",
|
13 |
+
).set(
|
14 |
+
background_fill_primary_dark='*neutral_950',
|
15 |
+
background_fill_secondary_dark='*neutral_950'
|
16 |
+
)
|
17 |
|
18 |
+
from urllib.parse import parse_qs
|
19 |
+
def parse_query_params(query_string):
|
20 |
+
parsed_params = parse_qs(query_string)
|
21 |
+
simplified_params = {k: v[0] if len(v) == 1 else v for k, v in parsed_params.items()}
|
22 |
+
return simplified_params
|
23 |
|
24 |
+
def app_inits():
|
25 |
+
global qs
|
26 |
+
|
27 |
+
# List of JSON file names
|
28 |
+
filenames = ['fileone.json', 'filesecond.json', 'filethird.json', 'filefourth.json', 'filefifth.json']
|
29 |
+
|
30 |
+
# Load data from each JSON file
|
31 |
+
for filename in filenames:
|
32 |
+
with open(filename, 'r') as file:
|
33 |
+
data = json.load(file)
|
34 |
+
qs.update(data)
|
35 |
+
|
36 |
+
print("Loaded Q & A")
|
37 |
+
print("Keys:", list(qs.keys()))
|
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 = theme) as demo:
|
43 |
+
chatbot = gr.Chatbot(elem_id="chatbot", layout = "panel", avatar_images=("images/user.jpg", "images/bot.jpg"),)
|
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 = 5,container = False,)
|
55 |
+
btn = gr.Button(value="", min_width=80, size = "lg", icon="images/button.jpg", scale= 0)
|
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( history, url_params):
|
65 |
+
global qs
|
66 |
+
DelayBetweenWords = 0.1
|
67 |
+
DelayBetweenLetters = 0.0001
|
68 |
|
69 |
+
if url_params != "":
|
70 |
+
params = parse_query_params(url_params)
|
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 |
+
if search_keyword == "None":
|
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 |
+
if text == "debug":
|
95 |
+
bot_message = "[DEBUG (log file: " + full_path + " params: " + url_params + "\nWords Delay=" + str(DelayBetweenWords) + " sec. Letters Delay=" + str(DelayBetweenLetters) + " sec.) \nKeys:\n" + "\n".join(keywords) + "]"
|
96 |
+
|
97 |
+
history[-1][1] = ""
|
98 |
+
|
99 |
+
for character in bot_message:
|
100 |
+
history[-1][1] += character
|
101 |
+
|
102 |
+
if character == " ":
|
103 |
+
time.sleep(DelayBetweenWords)
|
104 |
+
else:
|
105 |
+
time.sleep(DelayBetweenLetters)
|
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 |
+
)
|
114 |
+
|
115 |
+
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False, show_progress=False).then(
|
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'
|
173 |
+
|
174 |
+
def log_message(param):
|
175 |
+
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
176 |
+
log_msg = f'{timestamp}: : {param}\n'
|
177 |
+
|
178 |
+
with open(filename, 'a') as log_file:
|
179 |
+
log_file.write(log_msg)
|
180 |
+
|
181 |
+
app_inits()
|
182 |
+
demo.queue()
|
183 |
+
demo.launch(allowed_paths=["."], max_threads=40)
|