İlbey GÜLMEZ commited on
Commit
34eb72e
·
1 Parent(s): 017a621

Updatea app.py

Browse files
Files changed (3) hide show
  1. Dockerfile +16 -0
  2. app.py +71 -4
  3. requirements.txt +7 -2
Dockerfile ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10-slim
2
+
3
+ RUN useradd user
4
+
5
+ USER user
6
+
7
+ ENV HOME=/home/user \
8
+ PATH=/home/user/.local/bin:$PATH
9
+
10
+ WORKDIR $HOME/app
11
+
12
+ COPY --chown=user ./ $HOME/app
13
+
14
+ RUN pip install -r requirements.txt
15
+
16
+ CMD fastapi run --reload --host=0.0.0.0 --port=7860
app.py CHANGED
@@ -1,7 +1,74 @@
1
- from fastapi import FastAPI
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  app = FastAPI()
4
 
5
- @app.get("/")
6
- def greet_json():
7
- return {"Hello": "World!"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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}
requirements.txt CHANGED
@@ -1,2 +1,7 @@
1
- fastapi
2
- uvicorn[standard]
 
 
 
 
 
 
1
+ fastapi[standard]
2
+ uvicorn
3
+ transformers
4
+ torch
5
+ accelerate>=0.26.0
6
+ huggingface-hub
7
+ debugpy