chat / app.py
gampala1234's picture
Update app.py
3dadeb5 verified
Raw History Blame
1.32 kB
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
# Load the model and tokenizer
model_id = "Qwen/Qwen3-0.6B"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float32)
def chat(message, history):
# Format the history in the new messages format
messages = []
for user_msg, bot_msg in history:
messages.append({"role": "user", "content": user_msg})
messages.append({"role": "assistant", "content": bot_msg})
messages.append({"role": "user", "content": message})
# Prepare input for the model
inputs = tokenizer(message, return_tensors="pt")
outputs = model.generate(
inputs["input_ids"],
attention_mask=inputs["attention_mask"],
max_new_tokens=150,
do_sample=True,
top_p=0.9,
temperature=0.7
)
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
return response
# Create the chat interface with the new messages format
demo = gr.ChatInterface(
fn=chat,
title="Maya AI (Lite)",
theme="soft",
# Updated Chatbot configuration without the invalid 'likeable' parameter
chatbot=gr.Chatbot(height=500, layout="panel", type="messages")
)
demo.launch()