File size: 1,623 Bytes
f34412f
 
86ec5e1
f34412f
86ec5e1
 
 
 
 
f34412f
86ec5e1
 
 
 
 
 
f34412f
 
86ec5e1
 
 
f34412f
 
86ec5e1
 
 
 
 
 
 
 
 
f34412f
86ec5e1
 
 
f34412f
 
86ec5e1
 
 
f34412f
86ec5e1
f34412f
 
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
import gradio as gr
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM

tokenizer = AutoTokenizer.from_pretrained("HooshvareLab/t5-fa-small", use_fast=True)
model = AutoModelForCausalLM.from_pretrained(
    "HooshvareLab/t5-fa-small",
    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()