Spaces:
Sleeping
Sleeping
BeveledCube
commited on
Commit
•
22cf642
1
Parent(s):
8105f04
Added hermes model
Browse files- main.py +15 -3
- models/hermes.py +18 -0
main.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
from flask import Flask, request, render_template, jsonify
|
2 |
-
from models import
|
3 |
|
4 |
app = Flask("AI API")
|
5 |
|
@@ -15,8 +15,20 @@ def test_route():
|
|
15 |
def receive_data():
|
16 |
data = request.get_json()
|
17 |
print("Prompt:", data["prompt"])
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
print("Response:", generated_text)
|
22 |
|
|
|
1 |
from flask import Flask, request, render_template, jsonify
|
2 |
+
from models import hermes
|
3 |
|
4 |
app = Flask("AI API")
|
5 |
|
|
|
15 |
def receive_data():
|
16 |
data = request.get_json()
|
17 |
print("Prompt:", data["prompt"])
|
18 |
+
|
19 |
+
messages = []
|
20 |
+
|
21 |
+
if data["system"]:
|
22 |
+
messages.append({"role": "system", "content": data["system"] })
|
23 |
+
|
24 |
+
messages.append(
|
25 |
+
{
|
26 |
+
"role": "user",
|
27 |
+
"content": data["prompt"]
|
28 |
+
}
|
29 |
+
)
|
30 |
+
|
31 |
+
generated_text = hermes.generate(messages)
|
32 |
|
33 |
print("Response:", generated_text)
|
34 |
|
models/hermes.py
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
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 = [
|
10 |
+
# {"role": "system", "content": "You are Hermes 2."},
|
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)
|
17 |
+
|
18 |
+
return tokenizer.decode(output_ids[0], skip_special_tokens=True)
|