Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import torch | |
| from transformers import AutoTokenizer, AutoModelForCausalLM | |
| # Load local model | |
| tokenizer = AutoTokenizer.from_pretrained("vanilladucky/Friends_chatting_bot") | |
| model = AutoModelForCausalLM.from_pretrained("vanilladucky/Friends_chatting_bot") | |
| def chat(user_input, history=[]): | |
| conversation = "" | |
| for human, bot in history: | |
| conversation += f"User: {human}\nBot: {bot}\n" | |
| conversation += f"User: {user_input}\nBot:" | |
| inputs = tokenizer(conversation, return_tensors="pt").to(model.device) | |
| outputs = model.generate( | |
| **inputs, | |
| max_new_tokens=100, | |
| do_sample=True, | |
| temperature=0.7, | |
| top_p=0.9 | |
| ) | |
| response = tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:], skip_special_tokens=True).strip() | |
| history.append((user_input, response)) | |
| return history, history, "" # <-- return "" to clear OR user_input to keep text | |
| with gr.Blocks() as demo: | |
| gr.Markdown("CodeScape Demo") | |
| chatbot = gr.Chatbot() | |
| msg = gr.Textbox(label="Your message") | |
| clear = gr.Button("Clear") | |
| state = gr.State([]) | |
| msg.submit(chat, [msg, state], [chatbot, state, msg]) # also update msg | |
| clear.click(lambda: ([], []), None, [chatbot, state]) | |
| demo.launch() | |