Spaces:
Sleeping
Sleeping
import gradio as gr | |
import torch | |
from transformers import AutoTokenizer, AutoModelForCausalLM | |
tokenizer = AutoTokenizer.from_pretrained("HooshvareLab/gpt2-fa", use_fast=True) | |
model = AutoModelForCausalLM.from_pretrained( | |
"HooshvareLab/gpt2-fa", | |
torch_dtype=torch.bfloat16 | |
).to("cpu") | |
CONTEXT = ( | |
"This is a conversation with ParvizGPT. It is an artificial intelligence model designed by Amir Mahdi Parviz, " | |
"an NLP expert, to help you with various tasks such as answering questions, " | |
"providing recommendations, and assisting with decision-making. Ask it anything!" | |
) | |
pretokenized_context = tokenizer(CONTEXT, return_tensors="pt").input_ids.to("cpu") | |
def generate_response(message, chat_history): | |
prompt = torch.cat( | |
[pretokenized_context, tokenizer("\nYou: " + message + "\nParvizGPT: ", return_tensors="pt").input_ids.to("cpu")], | |
dim=1 | |
) | |
with torch.no_grad(): | |
outputs = model.generate( | |
prompt, | |
max_new_tokens=32, | |
temperature=0.6, | |
top_k=20, | |
top_p=0.8, | |
do_sample=True | |
) | |
result = tokenizer.decode(outputs[0], skip_special_tokens=True) | |
response = result.split("ParvizGPT:")[-1].strip() | |
return chat_history + [(message, response)] | |
with gr.Blocks() as demo: | |
gr.Markdown("<h1 style='text-align: center;'>💬 Parviz GPT</h1>") | |
chatbot = gr.Chatbot(label="Response") | |
msg = gr.Textbox(label="Input", placeholder="Ask your question...", lines=1) | |
msg.submit(generate_response, [msg, chatbot], chatbot) | |
gr.ClearButton([msg, chatbot]) | |
demo.launch() | |