File size: 1,354 Bytes
dc3e00d
11293c4
dc3e00d
 
 
 
11293c4
dc3e00d
11293c4
dc3e00d
 
11293c4
dc3e00d
 
 
 
 
11293c4
 
 
3b96f8b
11293c4
 
 
 
 
 
 
 
 
 
 
 
 
 
5545ad2
 
11293c4
 
 
 
 
dc3e00d
11293c4
bce18fa
dc3e00d
11293c4
0877c34
11293c4
0877c34
 
11293c4
 
 
 
 
 
 
 
0877c34
3b96f8b
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# =========================================
# FLAN-T5 Chatbot (100% Stable - FINAL)
# =========================================

import gradio as gr
import torch
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM

MODEL_NAME = "google/flan-t5-base"

tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)

device = "cuda" if torch.cuda.is_available() else "cpu"
model = model.to(device)


# -----------------------------
# Chat Function (IMPORTANT)
# -----------------------------
def chat(message, history):
    
    prompt = f"""
You are a helpful AI assistant.
Answer clearly and naturally.

User: {message}
Assistant:
"""

    inputs = tokenizer(
        prompt,
        return_tensors="pt",
        truncation=True,
        max_length=512
    ).to(device)

    outputs = model.generate(
        inputs.input_ids,
        max_length=120,
        temperature=0.7,
        top_p=0.9,
        do_sample=True,
        repetition_penalty=1.2
    )

    response = tokenizer.decode(outputs[0], skip_special_tokens=True)

    return response


# -----------------------------
# Gradio Chat Interface (🔥 FIX)
# -----------------------------
demo = gr.ChatInterface(
    fn=chat,
    title="🤖 AI Dialogue System (FLAN-T5)",
    description="Chat with AI using FLAN-T5"
)

demo.launch()