yxmauw commited on
Commit
d19cbee
1 Parent(s): 37e7669

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -3
app.py CHANGED
@@ -4,14 +4,19 @@ from urllib.request import urlopen
4
  import json
5
  import time
6
 
 
 
7
  url = "https://raw.githubusercontent.com/nomic-ai/gpt4all/main/gpt4all-chat/metadata/models3.json"
8
  response = urlopen(url)
9
  data_json = json.loads(response.read())
10
 
 
11
  def model_choices():
12
  model_list = [data_json[i]['filename'] for i in range(len(data_json))]
13
  return model_list
14
 
 
 
15
  model_description = {model['filename']: model['description'] for model in data_json}
16
 
17
 
@@ -20,7 +25,39 @@ def llm_intro(selected_model):
20
  formatted_description = html_string.replace("<ul>", "").replace("</ul>", "").replace("</li>", "").replace("<br>", "\n").replace("</br>", "").replace("<li>", "\n➤ ")
21
  return formatted_description
22
 
23
- model_cache = {} # Global cache
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
 
26
  def load_model(model_name):
@@ -36,7 +73,7 @@ def load_model(model_name):
36
 
37
  # clear = gr.ClearButton([input_text, chatbot])
38
 
39
- # Define the chatbot function
40
  def generate_response(model_name, message, chat_history):
41
  model = load_model(model_name)
42
  chat_history = []
@@ -49,7 +86,7 @@ def generate_response(model_name, message, chat_history):
49
  chat_history.append((input_text, response))
50
  return chat_history, response
51
 
52
- # Create the Gradio interface
53
  with gr.Blocks() as demo:
54
  gr.Markdown("# GPT4All Chatbot")
55
  with gr.Row():
 
4
  import json
5
  import time
6
 
7
+
8
+ # populate all models available from GPT4All
9
  url = "https://raw.githubusercontent.com/nomic-ai/gpt4all/main/gpt4all-chat/metadata/models3.json"
10
  response = urlopen(url)
11
  data_json = json.loads(response.read())
12
 
13
+
14
  def model_choices():
15
  model_list = [data_json[i]['filename'] for i in range(len(data_json))]
16
  return model_list
17
 
18
+
19
+ # get each models' description
20
  model_description = {model['filename']: model['description'] for model in data_json}
21
 
22
 
 
25
  formatted_description = html_string.replace("<ul>", "").replace("</ul>", "").replace("</li>", "").replace("<br>", "\n").replace("</br>", "").replace("<li>", "\n➤ ")
26
  return formatted_description
27
 
28
+
29
+ def remove_endtags(html_string, tags):
30
+ """Remove rear HTML tags from the input string."""
31
+ for tag in tags:
32
+ html_string = re.sub(fr"</{tag}>", "", html_string)
33
+ return html_string
34
+
35
+
36
+ def replace_starttags(html_string, replacements):
37
+ """Replace starting HTML tags with the corresponding values."""
38
+ for tag, replacement in replacements.items():
39
+ html_string = html_string.replace(tag, replacement)
40
+ return html_string
41
+
42
+
43
+ def format_html_string(html_string):
44
+ """Format the HTML string to a readable text format."""
45
+ tags_to_remove = ["ul", "li", "br"]
46
+ html_string = remove_endtags(html_string, tags_to_remove)
47
+
48
+ tag_replacements = {
49
+ "<li>": "\n➤ ",
50
+ "<br>": "\n",
51
+ "<strong>": "**",
52
+ "</strong>": "**"
53
+ }
54
+ formatted_string = replace_starttags(html_string, tag_replacements)
55
+
56
+ return formatted_string
57
+
58
+
59
+ # cache models for faster reloads
60
+ model_cache = {}
61
 
62
 
63
  def load_model(model_name):
 
73
 
74
  # clear = gr.ClearButton([input_text, chatbot])
75
 
76
+ # Construct chatbot
77
  def generate_response(model_name, message, chat_history):
78
  model = load_model(model_name)
79
  chat_history = []
 
86
  chat_history.append((input_text, response))
87
  return chat_history, response
88
 
89
+ # Create Gradio UI
90
  with gr.Blocks() as demo:
91
  gr.Markdown("# GPT4All Chatbot")
92
  with gr.Row():