|
|
import torch |
|
|
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline |
|
|
import gradio as gr |
|
|
|
|
|
|
|
|
torch.random.manual_seed(0) |
|
|
model = AutoModelForCausalLM.from_pretrained( |
|
|
"microsoft/Phi-3-mini-128k-instruct", |
|
|
device_map="cpu", |
|
|
torch_dtype="auto", |
|
|
trust_remote_code=True, |
|
|
attn_implementation="eager" |
|
|
) |
|
|
|
|
|
tokenizer = AutoTokenizer.from_pretrained("microsoft/Phi-3-mini-128k-instruct") |
|
|
|
|
|
|
|
|
pipe = pipeline( |
|
|
"text-generation", |
|
|
model=model, |
|
|
tokenizer=tokenizer, |
|
|
) |
|
|
|
|
|
|
|
|
def generate_response(input_text): |
|
|
messages = [ |
|
|
{"role": "system", "content": "You are a helpful AI assistant."}, |
|
|
{"role": "user", "content": input_text} |
|
|
] |
|
|
|
|
|
generation_args = { |
|
|
"max_new_tokens": 500, |
|
|
"return_full_text": False, |
|
|
"temperature": 0.7, |
|
|
"do_sample": True, |
|
|
} |
|
|
|
|
|
output = pipe(messages, **generation_args) |
|
|
return output[0]['generated_text'] |
|
|
|
|
|
|
|
|
with gr.Blocks() as demo: |
|
|
gr.Markdown("# AI Chatbot Assistant\nTanya apapun, saya siap membantu!") |
|
|
|
|
|
|
|
|
gr.Markdown( |
|
|
"### ⚠ Sorry for the inconvenience. The Space is currently running on the CPU, which might affect performance. We appreciate your understanding." |
|
|
) |
|
|
|
|
|
|
|
|
with gr.Row(): |
|
|
output_box = gr.Textbox( |
|
|
label="AI Response", |
|
|
placeholder="Respons akan muncul di sini...", |
|
|
lines=10, |
|
|
interactive=False |
|
|
) |
|
|
with gr.Row(): |
|
|
input_box = gr.Textbox(label="Ask me anything!", placeholder="Tanyakan sesuatu...") |
|
|
with gr.Row(): |
|
|
submit_button = gr.Button("Submit") |
|
|
|
|
|
|
|
|
submit_button.click(generate_response, inputs=input_box, outputs=output_box) |
|
|
|
|
|
|
|
|
demo.launch() |