CoolFace
Apppublic

Jitendra14355/Dialogue_System

sourceHugging Faceupdated 5mo agoView on Hugging Face
0likes
app.py61 linesDownload Raw Back to root
1# =========================================2# FLAN-T5 Chatbot (100% Stable - FINAL)3# =========================================4 5import gradio as gr6import torch7from transformers import AutoTokenizer, AutoModelForSeq2SeqLM8 9MODEL_NAME = "google/flan-t5-base"10 11tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)12model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)13 14device = "cuda" if torch.cuda.is_available() else "cpu"15model = model.to(device)16 17 18# -----------------------------19# Chat Function (IMPORTANT)20# -----------------------------21def chat(message, history):22    23    prompt = f"""24You are a helpful AI assistant.25Answer clearly and naturally.26 27User: {message}28Assistant:29"""30 31    inputs = tokenizer(32        prompt,33        return_tensors="pt",34        truncation=True,35        max_length=51236    ).to(device)37 38    outputs = model.generate(39        inputs.input_ids,40        max_length=120,41        temperature=0.7,42        top_p=0.9,43        do_sample=True,44        repetition_penalty=1.245    )46 47    response = tokenizer.decode(outputs[0], skip_special_tokens=True)48 49    return response50 51 52# -----------------------------53# Gradio Chat Interface (๐Ÿ”ฅ FIX)54# -----------------------------55demo = gr.ChatInterface(56    fn=chat,57    title="๐Ÿค– AI Dialogue System (FLAN-T5)",58    description="Chat with AI using FLAN-T5"59)60 61demo.launch()