Spaces:
Sleeping
Sleeping
keyword extraction
Browse files- app.py +12 -3
- app_cosmos.py +74 -0
app.py
CHANGED
@@ -17,8 +17,17 @@ 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
]
|
23 |
|
24 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
@@ -60,7 +69,7 @@ async def ask(request: Request):
|
|
60 |
print("Model process started")
|
61 |
outputs = model.generate(
|
62 |
input_ids,
|
63 |
-
max_new_tokens=
|
64 |
eos_token_id=terminators,
|
65 |
do_sample=True,
|
66 |
temperature=0.6,
|
|
|
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")
|
|
|
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,
|
app_cosmos.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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": "Sen bir yapay zeka asistanısın. Kullanıcı sana bir görev verecek. Amacın görevi olabildiğince sadık bir şekilde tamamlamak."}
|
21 |
+
# Görevi yerine getirirken adım adım düşün ve adımlarını gerekçelendir.
|
22 |
+
]
|
23 |
+
|
24 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
25 |
+
print("Selected device:", device)
|
26 |
+
|
27 |
+
app = FastAPI()
|
28 |
+
|
29 |
+
|
30 |
+
@app.get('/')
|
31 |
+
def home():
|
32 |
+
return {"hello": "Bitfumes"}
|
33 |
+
|
34 |
+
|
35 |
+
@app.post('/ask')
|
36 |
+
async def ask(request: Request):
|
37 |
+
data = await request.json()
|
38 |
+
prompt = data.get("prompt")
|
39 |
+
if not prompt:
|
40 |
+
return {"error": "Prompt is missing"}
|
41 |
+
|
42 |
+
print("Device of the model:", model.device)
|
43 |
+
messages = initial_message.copy()
|
44 |
+
messages.append({"role": "user", "content": f"{prompt}"})
|
45 |
+
|
46 |
+
print("Messages:", messages)
|
47 |
+
print("Tokenizer process started")
|
48 |
+
input_ids = tokenizer.apply_chat_template(
|
49 |
+
messages,
|
50 |
+
add_generation_prompt=True,
|
51 |
+
return_tensors="pt"
|
52 |
+
).to(model.device)
|
53 |
+
|
54 |
+
terminators = [
|
55 |
+
tokenizer.eos_token_id,
|
56 |
+
tokenizer.convert_tokens_to_ids("<|eot_id|>")
|
57 |
+
]
|
58 |
+
print("Tokenizer process completed")
|
59 |
+
|
60 |
+
print("Model process started")
|
61 |
+
outputs = model.generate(
|
62 |
+
input_ids,
|
63 |
+
max_new_tokens=256,
|
64 |
+
eos_token_id=terminators,
|
65 |
+
do_sample=True,
|
66 |
+
temperature=0.6,
|
67 |
+
top_p=0.9,
|
68 |
+
)
|
69 |
+
response = outputs[0][input_ids.shape[-1]:]
|
70 |
+
|
71 |
+
print("Tokenizer decode process started")
|
72 |
+
answer = tokenizer.decode(response, skip_special_tokens=True)
|
73 |
+
|
74 |
+
return {"answer": answer}
|