Spaces:
Paused
Paused
File size: 2,468 Bytes
f0ebd38 |
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
from trulens_eval import Tru, TruCustomApp, Feedback
from trulens_eval.tru_custom_app import instrument
from trulens_eval.feedback import Coherence, Relevance
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
class YourModel:
def __init__(self):
Tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen-1_8B-Chat", trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen-1_8B-Chat", device_map="auto", trust_remote_code=True).eval()
history = None
@instrument
def predict(self, user_input, system_prompt):
print("Model response for: " + user_input + " with prompt: " + system_prompt")
response, self.history = model.chat(tokenizer, user_input, history=self.history, system=system_prompt)
return response
tru = Tru()
tru.reset_database()
model = YourModel()
coherence_feedback = Feedback(Coherence()).on_output()
relevance_feedback = Feedback(Relevance()).on_input_output()
ta = TruCustomApp(model, app_id="trumodel", feedbacks=[coherence_feedback, relevance_feedback])
def make_predictions(inputs):
for user_input, system_prompt in inputs:
response, record = ta.with_record(model.predict, user_input, system_prompt)
print(response)
print(record.dict())
inputs = [
("Hello!", "Hello!"),
("How are you?", "How are you?"),
("What's the weather like?", "What's the weather like?"),
("Tell me a joke.", "Tell me a joke."),
("What's new today?", "What's new today?"),
("Can you give me some advice?", "Can you give me some advice?"),
("What's your favorite color?", "What's your favorite color?"),
("How does AI work?", "How does AI work?"),
("What's the meaning of life?", "What's the meaning of life?"),
("Do you like music?", "Do you like music?"),
("What's the latest news?", "What's the latest news?"),
("Can you recommend a book?", "Can you recommend a book?"),
("What time is it?", "What time is it?"),
("Tell me about yourself.", "Tell me about yourself."),
("What's your favorite food?", "What's your favorite food?"),
("How do you learn?", "How do you learn?"),
("What are your hobbies?", "What are your hobbies?"),
("Can you tell a story?", "Can you tell a story?"),
("What is your opinion on art?", "What is your opinion on art?"),
("Do you know any fun facts?", "Do you know any fun facts?")
]
make_predictions(inputs)
tru.start_dashboard() |