File size: 1,381 Bytes
6c00582 |
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 |
from transformers import pipeline
from transformers import AutoModelForCausalLM, AutoTokenizer
import gradio as gr
from nltk.tokenize import sent_tokenize
import torch
model = "janny127/autotrain-5e45b-p5z66"
tokenizer = AutoTokenizer.from_pretrained(model)
pipeline = pipeline(
"text-generation",
model=model,
torch_dtype=torch.float16,
device_map="auto",
)
def predict(prompt, history):
# Prompt
formatted_prompt = (
f"### Human: {prompt}### Assistant:"
)
# Generate the Texts
sequences = pipeline(
formatted_prompt,
do_sample=True,
top_k=50,
top_p = 0.7,
num_return_sequences=1,
repetition_penalty=1.1,
max_new_tokens=500,
)
generated_text = sequences[0]['generated_text']
final_result = generated_text.split("### Assistant:")[1]
if " Human: " in final_result:
final_result = final_result.split(" Human: ")[0]
if " #" in final_result:
final_result = final_result.split(" #")[0]
# return generated_text.strip()
return final_result.strip()
gr.ChatInterface(predict,
title="Tinyllama_chatBot",
description="Ask Tiny llama any questions",
examples=['How to cook a fish?', 'Who is the president of US now?']
).launch() # Launching the web interface.
|