File size: 3,528 Bytes
6f78863
8605573
6f78863
 
8605573
6f78863
 
8431cf4
6f78863
 
 
 
8605573
6f78863
 
 
8605573
6f78863
 
 
8605573
 
 
 
 
 
 
 
1ec8383
6f78863
 
 
8431cf4
8605573
 
dda6a92
 
8605573
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dda6a92
8605573
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dda6a92
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM, AutoModelForSeq2SeqLM
from PyPDF2 import PdfReader

# Models and Tokenizers Setup
models = {
    "Text Generator (Bloom)": {
        "model": AutoModelForCausalLM.from_pretrained("bigscience/bloom-560m"),
        "tokenizer": AutoTokenizer.from_pretrained("bigscience/bloom-560m"),
    },
    "PDF Summarizer (T5)": {
        "model": AutoModelForSeq2SeqLM.from_pretrained("t5-small"),
        "tokenizer": AutoTokenizer.from_pretrained("t5-small", use_fast=False),
    },
    "Broken Answer (T0pp)": {
        "model": AutoModelForSeq2SeqLM.from_pretrained("bigscience/T0pp"),
        "tokenizer": AutoTokenizer.from_pretrained("bigscience/T0pp", use_fast=False),
    },
}

# Chat Function
def chat_with_model(model_choice, user_message, chat_history, file=None):
    if model_choice == "PDF Summarizer (T5)" and file is not None:
        pdf_text = extract_text_from_pdf(file)
        user_message += f"\n\nPDF Content:\n{pdf_text}"

    if not user_message.strip():
        return chat_history

    model_info = models[model_choice]
    tokenizer = model_info["tokenizer"]
    model = model_info["model"]

    # Tokenize Input
    inputs = tokenizer(user_message, return_tensors="pt", padding=True, truncation=True, max_length=512)
    # Generate Output
    outputs = model.generate(**inputs, max_length=150, num_beams=5, early_stopping=True)
    response = tokenizer.decode(outputs[0], skip_special_tokens=True)

    # Update Chat History
    chat_history.append((user_message, response))
    return chat_history

# Function to Extract Text from PDF
def extract_text_from_pdf(file):
    from PyPDF2 import PdfReader
    reader = PdfReader(file.name)
    text = "\n".join(page.extract_text() for page in reader.pages if page.extract_text())
    return text

# Interface Setup
def create_chat_interface():
    with gr.Blocks(css="""
        .chatbox {
            background-color: #f7f7f8;
            border-radius: 12px;
            padding: 16px;
            font-family: 'Segoe UI', Tahoma, sans-serif;
        }
        .chat-title {
            font-size: 24px;
            font-weight: bold;
            text-align: center;
            margin-bottom: 12px;
            color: #3a9fd6;
        }
    """) as interface:
        gr.Markdown("<div class='chat-title'>GPT-Style Chat Interface</div>")

        with gr.Row():
            model_choice = gr.Dropdown(
                choices=list(models.keys()),
                value="Text Generator (Bloom)",
                label="Select Model"
            )

        chat_history = gr.Chatbot(label="Chat History", elem_classes="chatbox")

        user_message = gr.Textbox(
            placeholder="Type your message here...",
            show_label=False,
            elem_classes="chatbox",
        )

        file_input = gr.File(label="Upload PDF", visible=False, file_types=[".pdf"])

        def toggle_pdf_input(selected_model):
            return gr.update(visible=(selected_model == "PDF Summarizer (T5)"))

        model_choice.change(fn=toggle_pdf_input, inputs=model_choice, outputs=file_input)

        send_button = gr.Button("Send")

        # Link the send button to the chat function
        send_button.click(
            chat_with_model,
            inputs=[model_choice, user_message, chat_history, file_input],
            outputs=chat_history,
        )

    return interface

if __name__ == "__main__":
    interface = create_chat_interface()
    interface.launch()