Spaces:
Sleeping
Sleeping
activate NER extraction
Browse files- app.py +41 -15
- app_keyword_ner.py → app_gemma.py +15 -41
app.py
CHANGED
@@ -1,26 +1,35 @@
|
|
1 |
from fastapi import FastAPI, Request
|
2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
import torch
|
4 |
-
from huggingface_hub import login
|
5 |
-
import os
|
6 |
|
7 |
-
print("
|
8 |
|
9 |
-
|
10 |
-
access_token = os.getenv('HF_TOKEN')
|
11 |
-
login(access_token)
|
12 |
-
|
13 |
-
model_id = "google/gemma-2-9b-it"
|
14 |
|
15 |
print("Model loading started")
|
16 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
17 |
model = AutoModelForCausalLM.from_pretrained(
|
18 |
model_id,
|
19 |
-
device_map="auto",
|
20 |
torch_dtype=torch.bfloat16,
|
|
|
21 |
)
|
22 |
print("Model loading completed")
|
23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
25 |
print("Selected device:", device)
|
26 |
|
@@ -40,18 +49,35 @@ async def ask(request: Request):
|
|
40 |
return {"error": "Prompt is missing"}
|
41 |
|
42 |
print("Device of the model:", model.device)
|
43 |
-
messages =
|
44 |
-
|
45 |
-
|
46 |
print("Messages:", messages)
|
47 |
print("Tokenizer process started")
|
48 |
-
input_ids = tokenizer.apply_chat_template(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
print("Tokenizer process completed")
|
50 |
|
51 |
print("Model process started")
|
52 |
-
outputs = model.generate(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
print("Tokenizer decode process started")
|
55 |
-
answer = tokenizer.decode(
|
56 |
|
57 |
return {"answer": answer}
|
|
|
1 |
from fastapi import FastAPI, Request
|
2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
import torch
|
|
|
|
|
4 |
|
5 |
+
print("COSMOS Llama Chatbot is starting...")
|
6 |
|
7 |
+
model_id = "ytu-ce-cosmos/Turkish-Llama-8b-DPO-v0.1"
|
|
|
|
|
|
|
|
|
8 |
|
9 |
print("Model loading started")
|
10 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
11 |
model = AutoModelForCausalLM.from_pretrained(
|
12 |
model_id,
|
|
|
13 |
torch_dtype=torch.bfloat16,
|
14 |
+
device_map="auto",
|
15 |
)
|
16 |
print("Model loading completed")
|
17 |
|
18 |
+
# bu mesaj değiştirilebilir ve chatbotun başlangıç mesajı olarak kullanılabilir
|
19 |
+
initial_message = [
|
20 |
+
{"role": "system", "content":
|
21 |
+
"""Kullanıcı sana bir haber metni verecek. Bu haber metninin önemli kısımlarını özetleyen 5 cümle çıkart. Aynı zamanda bu cümlelerin her birinden bir keyword extract et ve eğer varsa NER ile yer, kişi, tarih gibi alanları extract et. Yoksa karşısını boş bırak. Çıktıların şu formatta olsun:
|
22 |
+
1. Cümle: Cumhurbaşkanı Erdoğan tatile çıktı.
|
23 |
+
Keyword: tatil
|
24 |
+
NER: Cumhurbaşkanı Erdoğan
|
25 |
+
|
26 |
+
2. Cümle: ...
|
27 |
+
Keyword: ...
|
28 |
+
NER: ...
|
29 |
+
"""
|
30 |
+
}
|
31 |
+
]
|
32 |
+
|
33 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
34 |
print("Selected device:", device)
|
35 |
|
|
|
49 |
return {"error": "Prompt is missing"}
|
50 |
|
51 |
print("Device of the model:", model.device)
|
52 |
+
messages = initial_message.copy()
|
53 |
+
messages.append({"role": "user", "content": f"{prompt}"})
|
54 |
+
|
55 |
print("Messages:", messages)
|
56 |
print("Tokenizer process started")
|
57 |
+
input_ids = tokenizer.apply_chat_template(
|
58 |
+
messages,
|
59 |
+
add_generation_prompt=True,
|
60 |
+
return_tensors="pt"
|
61 |
+
).to(model.device)
|
62 |
+
|
63 |
+
terminators = [
|
64 |
+
tokenizer.eos_token_id,
|
65 |
+
tokenizer.convert_tokens_to_ids("<|eot_id|>")
|
66 |
+
]
|
67 |
print("Tokenizer process completed")
|
68 |
|
69 |
print("Model process started")
|
70 |
+
outputs = model.generate(
|
71 |
+
input_ids,
|
72 |
+
max_new_tokens=512,
|
73 |
+
eos_token_id=terminators,
|
74 |
+
do_sample=True,
|
75 |
+
temperature=0.6,
|
76 |
+
top_p=0.9,
|
77 |
+
)
|
78 |
+
response = outputs[0][input_ids.shape[-1]:]
|
79 |
|
80 |
print("Tokenizer decode process started")
|
81 |
+
answer = tokenizer.decode(response, skip_special_tokens=True)
|
82 |
|
83 |
return {"answer": answer}
|
app_keyword_ner.py → app_gemma.py
RENAMED
@@ -1,35 +1,26 @@
|
|
1 |
from fastapi import FastAPI, Request
|
2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
import torch
|
|
|
|
|
4 |
|
5 |
-
print("
|
6 |
|
7 |
-
|
|
|
|
|
|
|
|
|
8 |
|
9 |
print("Model loading started")
|
10 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
11 |
model = AutoModelForCausalLM.from_pretrained(
|
12 |
model_id,
|
13 |
-
torch_dtype=torch.bfloat16,
|
14 |
device_map="auto",
|
|
|
15 |
)
|
16 |
print("Model loading completed")
|
17 |
|
18 |
-
# bu mesaj değiştirilebilir ve chatbotun başlangıç mesajı olarak kullanılabilir
|
19 |
-
initial_message = [
|
20 |
-
{"role": "system", "content":
|
21 |
-
"""Kullanıcı sana bir haber metni verecek. Bu haber metninin önemli kısımlarını özetleyen 5 cümle çıkart. Aynı zamanda bu cümlelerin her birinden bir keyword extract et ve eğer varsa NER ile yer, kişi, tarih gibi alanları extract et. Yoksa karşısını boş bırak. Çıktıların şu formatta olsun:
|
22 |
-
1. Cümle: Cumhurbaşkanı Erdoğan tatile çıktı.
|
23 |
-
Keyword: tatil
|
24 |
-
NER: Cumhurbaşkanı Erdoğan
|
25 |
-
|
26 |
-
2. Cümle: ...
|
27 |
-
Keyword: ...
|
28 |
-
NER: ...
|
29 |
-
"""
|
30 |
-
}
|
31 |
-
]
|
32 |
-
|
33 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
34 |
print("Selected device:", device)
|
35 |
|
@@ -49,35 +40,18 @@ async def ask(request: Request):
|
|
49 |
return {"error": "Prompt is missing"}
|
50 |
|
51 |
print("Device of the model:", model.device)
|
52 |
-
messages =
|
53 |
-
|
54 |
-
|
55 |
print("Messages:", messages)
|
56 |
print("Tokenizer process started")
|
57 |
-
input_ids = tokenizer.apply_chat_template(
|
58 |
-
messages,
|
59 |
-
add_generation_prompt=True,
|
60 |
-
return_tensors="pt"
|
61 |
-
).to(model.device)
|
62 |
-
|
63 |
-
terminators = [
|
64 |
-
tokenizer.eos_token_id,
|
65 |
-
tokenizer.convert_tokens_to_ids("<|eot_id|>")
|
66 |
-
]
|
67 |
print("Tokenizer process completed")
|
68 |
|
69 |
print("Model process started")
|
70 |
-
outputs = model.generate(
|
71 |
-
input_ids,
|
72 |
-
max_new_tokens=512,
|
73 |
-
eos_token_id=terminators,
|
74 |
-
do_sample=True,
|
75 |
-
temperature=0.6,
|
76 |
-
top_p=0.9,
|
77 |
-
)
|
78 |
-
response = outputs[0][input_ids.shape[-1]:]
|
79 |
|
80 |
print("Tokenizer decode process started")
|
81 |
-
answer = tokenizer.decode(
|
82 |
|
83 |
return {"answer": answer}
|
|
|
1 |
from fastapi import FastAPI, Request
|
2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
import torch
|
4 |
+
from huggingface_hub import login
|
5 |
+
import os
|
6 |
|
7 |
+
print("Google Gemma 2 Chatbot is starting...")
|
8 |
|
9 |
+
# read access token from environment variable
|
10 |
+
access_token = os.getenv('HF_TOKEN')
|
11 |
+
login(access_token)
|
12 |
+
|
13 |
+
model_id = "google/gemma-2-9b-it"
|
14 |
|
15 |
print("Model loading started")
|
16 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
17 |
model = AutoModelForCausalLM.from_pretrained(
|
18 |
model_id,
|
|
|
19 |
device_map="auto",
|
20 |
+
torch_dtype=torch.bfloat16,
|
21 |
)
|
22 |
print("Model loading completed")
|
23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
25 |
print("Selected device:", device)
|
26 |
|
|
|
40 |
return {"error": "Prompt is missing"}
|
41 |
|
42 |
print("Device of the model:", model.device)
|
43 |
+
messages = [
|
44 |
+
{"role": "user", "content": f"{prompt}"},
|
45 |
+
]
|
46 |
print("Messages:", messages)
|
47 |
print("Tokenizer process started")
|
48 |
+
input_ids = tokenizer.apply_chat_template(messages, return_tensors="pt", return_dict=True).to("cuda")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
print("Tokenizer process completed")
|
50 |
|
51 |
print("Model process started")
|
52 |
+
outputs = model.generate(**input_ids, max_new_tokens=512)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
print("Tokenizer decode process started")
|
55 |
+
answer = tokenizer.decode(outputs[0]).split("<end_of_turn>")[1].strip()
|
56 |
|
57 |
return {"answer": answer}
|