-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOutlines_for_transformers.py
More file actions
31 lines (25 loc) · 947 Bytes
/
Copy pathOutlines_for_transformers.py
File metadata and controls
31 lines (25 loc) · 947 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import json
import outlines
import torch
from typing import List, Optional
from pydantic import BaseModel, Field
class ResponseFormat(BaseModel):
name : str
age: int
if torch.cuda.is_available():
print("GPU is Used!")
else:
print("CPU is Used!")
repo_id = "Qwen/Qwen2.5-0.5B-Instruct"
model = outlines.models.transformers(repo_id,
device="cuda",
model_kwargs={"temperature":0.5})
inputs = [
"give me name and age of the person as json. \"I\'m vishva, I'm 26 years old '\"",
"give me name and age of the person as json. \"I\'m Vimal, I'm 45 years old '\"",
"give me name and age of the person as json. \"I\'m Ragul, I'm 31 years old '\""
]
schema_as_str = json.dumps(ResponseFormat.model_json_schema())
generator = outlines.generate.json(model, schema_as_str)
output = generator(inputs)
print(json.dumps(output,indent=4))