Spaces:
Sleeping
Sleeping
initial commit
Browse files- app.py +52 -0
- requirements.txt +8 -0
app.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import spaces
|
3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
4 |
+
import torch
|
5 |
+
from huggingface_hub import login
|
6 |
+
import os
|
7 |
+
|
8 |
+
access_token = os.getenv('HF_TOKEN')
|
9 |
+
login(access_token)
|
10 |
+
|
11 |
+
model_id = "google/gemma-2-9b-it"
|
12 |
+
tokenizer = None
|
13 |
+
model = None
|
14 |
+
|
15 |
+
@spaces.GPU
|
16 |
+
def load_model():
|
17 |
+
global tokenizer, model
|
18 |
+
print("Model loading started")
|
19 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
20 |
+
model = AutoModelForCausalLM.from_pretrained(
|
21 |
+
model_id,
|
22 |
+
device_map="auto",
|
23 |
+
torch_dtype=torch.bfloat16,
|
24 |
+
)
|
25 |
+
print("Model loading completed. Device of the model:", model.device)
|
26 |
+
|
27 |
+
load_model()
|
28 |
+
|
29 |
+
@spaces.GPU
|
30 |
+
def ask(prompt):
|
31 |
+
if not prompt:
|
32 |
+
return {"error": "Prompt is missing"}
|
33 |
+
|
34 |
+
print("Device of the model:", model.device)
|
35 |
+
messages = [
|
36 |
+
{"role": "user", "content": f"{prompt}"},
|
37 |
+
]
|
38 |
+
print("Messages:", messages)
|
39 |
+
print("Tokenizer process started")
|
40 |
+
input_ids = tokenizer.apply_chat_template(messages, return_tensors="pt", return_dict=True).to("cuda")
|
41 |
+
print("Tokenizer process completed")
|
42 |
+
|
43 |
+
print("Model process started")
|
44 |
+
outputs = model.generate(**input_ids, max_new_tokens=256)
|
45 |
+
|
46 |
+
print("Tokenizer decode process started")
|
47 |
+
answer = tokenizer.decode(outputs[0]).split("<end_of_turn>")[1].strip()
|
48 |
+
|
49 |
+
return answer
|
50 |
+
|
51 |
+
demo = gr.Interface(fn=ask, inputs=gr.Textbox(lines=2, placeholder="Enter your prompt here..."), outputs=gr.Textbox())
|
52 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
transformers
|
3 |
+
torch
|
4 |
+
huggingface_hub
|
5 |
+
requests
|
6 |
+
gradio_client
|
7 |
+
fastapi
|
8 |
+
uvicorn
|