BeveledCube commited on
Commit
5f6d4f4
·
1 Parent(s): 838669d

Added gpt2 and fixed shi

Browse files
Files changed (5) hide show
  1. main.py +5 -2
  2. models/blenderbot.py +6 -2
  3. models/gpt2.py +21 -0
  4. models/hermes.py +9 -2
  5. models/llama3.py +6 -2
main.py CHANGED
@@ -1,8 +1,11 @@
1
  from flask import Flask, request, render_template, jsonify
2
- from models import blenderbot
3
 
4
  app = Flask("AI API")
5
 
 
 
 
6
  @app.get("/")
7
  def read_root():
8
  return render_template("index.html")
@@ -16,7 +19,7 @@ def receive_data():
16
  data = request.get_json()
17
  print("Prompt:", data["prompt"])
18
 
19
- generated_text = blenderbot.generate(data["prompt"])
20
 
21
  print("Response:", generated_text)
22
 
 
1
  from flask import Flask, request, render_template, jsonify
2
+ from models import gpt2 as chatbot
3
 
4
  app = Flask("AI API")
5
 
6
+ pring("Loading model")
7
+ chatbot.load()
8
+
9
  @app.get("/")
10
  def read_root():
11
  return render_template("index.html")
 
19
  data = request.get_json()
20
  print("Prompt:", data["prompt"])
21
 
22
+ generated_text = chatbot.generate(data["prompt"])
23
 
24
  print("Response:", generated_text)
25
 
models/blenderbot.py CHANGED
@@ -11,8 +11,12 @@ model_name = "facebook/blenderbot-1B-distill"
11
  # facebook/blenderbot-90M
12
  # facebook/blenderbot_small-90M
13
 
14
- model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
15
- tokenizer = AutoTokenizer.from_pretrained(model_name)
 
 
 
 
16
 
17
  def generate(input_text):
18
  # Tokenize the input text
 
11
  # facebook/blenderbot-90M
12
  # facebook/blenderbot_small-90M
13
 
14
+ def load():
15
+ global model
16
+ global tokenizer
17
+
18
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
19
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
20
 
21
  def generate(input_text):
22
  # Tokenize the input text
models/gpt2.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
2
+
3
+ # https://www.youtube.com/watch?v=irjYqV6EebU
4
+
5
+ model_name = "gpt2"
6
+
7
+ def load():
8
+ global model
9
+ global tokenizer
10
+
11
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
12
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
13
+
14
+ def generate(input_text):
15
+ # Tokenize the input text
16
+ input_ids = tokenizer.encode(input_text, return_tensors="pt")
17
+
18
+ # Generate output using the model
19
+ output_ids = model.generate(input_ids, num_beams=5, no_repeat_ngram_size=2)
20
+
21
+ return tokenizer.decode(output_ids[0], skip_special_tokens=True)
models/hermes.py CHANGED
@@ -2,8 +2,8 @@ from transformers import AutoTokenizer, AutoModelForCausalLM
2
 
3
  model_name = "NousResearch/Hermes-2-Pro-Llama-3-8B"
4
 
5
- model = AutoModelForCausalLM.from_pretrained(model_name)
6
- tokenizer = AutoTokenizer.from_pretrained(model_name)
7
 
8
  # Example messages input
9
  # messages = [
@@ -11,6 +11,13 @@ tokenizer = AutoTokenizer.from_pretrained(model_name)
11
  # {"role": "user", "content": "Hello, who are you?"}
12
  #]
13
 
 
 
 
 
 
 
 
14
  def generate(messages):
15
  gen_input = tokenizer.apply_chat_template(messages, return_tensors="pt")
16
  output_ids = model.generate(**gen_input, num_beams=5, no_repeat_ngram_size=2)
 
2
 
3
  model_name = "NousResearch/Hermes-2-Pro-Llama-3-8B"
4
 
5
+ model = None
6
+ tokenizer = None
7
 
8
  # Example messages input
9
  # messages = [
 
11
  # {"role": "user", "content": "Hello, who are you?"}
12
  #]
13
 
14
+ def load():
15
+ global model
16
+ global tokenizer
17
+
18
+ model = AutoModelForCausalLM.from_pretrained(model_name)
19
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
20
+
21
  def generate(messages):
22
  gen_input = tokenizer.apply_chat_template(messages, return_tensors="pt")
23
  output_ids = model.generate(**gen_input, num_beams=5, no_repeat_ngram_size=2)
models/llama3.py CHANGED
@@ -2,8 +2,12 @@ from transformers import AutoTokenizer, AutoModelForCausalLM
2
 
3
  model_name = "meta-llama/Meta-Llama-3-8B"
4
 
5
- tokenizer = AutoTokenizer.from_pretrained(model_name)
6
- model = AutoModelForCausalLM.from_pretrained(model_name)
 
 
 
 
7
 
8
  def generate(input_text):
9
  input_ids = tokenizer.encode(input_text, return_tensors="pt")
 
2
 
3
  model_name = "meta-llama/Meta-Llama-3-8B"
4
 
5
+ def load():
6
+ global model
7
+ global tokenizer
8
+
9
+ model = AutoModelForCausalLM.from_pretrained(model_name)
10
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
11
 
12
  def generate(input_text):
13
  input_ids = tokenizer.encode(input_text, return_tensors="pt")