changed gui
Browse files
app.py
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import AutoTokenizer,
|
| 3 |
from PyPDF2 import PdfReader
|
| 4 |
|
| 5 |
-
# Models and
|
| 6 |
models = {
|
| 7 |
"Text Generator (Bloom)": {
|
| 8 |
"model": AutoModelForCausalLM.from_pretrained("bigscience/bloom-560m"),
|
|
@@ -10,98 +10,96 @@ models = {
|
|
| 10 |
},
|
| 11 |
"PDF Summarizer (T5)": {
|
| 12 |
"model": AutoModelForSeq2SeqLM.from_pretrained("t5-small"),
|
| 13 |
-
"tokenizer": AutoTokenizer.from_pretrained("t5-small", use_fast=False),
|
| 14 |
},
|
| 15 |
"Broken Answer (T0pp)": {
|
| 16 |
"model": AutoModelForSeq2SeqLM.from_pretrained("bigscience/T0pp"),
|
| 17 |
-
"tokenizer": AutoTokenizer.from_pretrained("bigscience/T0pp", use_fast=False),
|
| 18 |
},
|
| 19 |
}
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
# Function for text generation
|
| 23 |
-
def generate_text(model_choice, input_text, max_tokens, temperature, top_p):
|
| 24 |
model_info = models[model_choice]
|
| 25 |
tokenizer = model_info["tokenizer"]
|
| 26 |
model = model_info["model"]
|
| 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 |
-
inputs=[broken_input, broken_max_tokens, broken_temperature, broken_top_p],
|
| 101 |
-
outputs=broken_output
|
| 102 |
-
)
|
| 103 |
-
|
| 104 |
-
demo.launch()
|
| 105 |
-
|
| 106 |
-
# Launch the app
|
| 107 |
-
launch_custom_app()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, AutoModelForSeq2SeqLM
|
| 3 |
from PyPDF2 import PdfReader
|
| 4 |
|
| 5 |
+
# Models and Tokenizers Setup
|
| 6 |
models = {
|
| 7 |
"Text Generator (Bloom)": {
|
| 8 |
"model": AutoModelForCausalLM.from_pretrained("bigscience/bloom-560m"),
|
|
|
|
| 10 |
},
|
| 11 |
"PDF Summarizer (T5)": {
|
| 12 |
"model": AutoModelForSeq2SeqLM.from_pretrained("t5-small"),
|
| 13 |
+
"tokenizer": AutoTokenizer.from_pretrained("t5-small", use_fast=False),
|
| 14 |
},
|
| 15 |
"Broken Answer (T0pp)": {
|
| 16 |
"model": AutoModelForSeq2SeqLM.from_pretrained("bigscience/T0pp"),
|
| 17 |
+
"tokenizer": AutoTokenizer.from_pretrained("bigscience/T0pp", use_fast=False),
|
| 18 |
},
|
| 19 |
}
|
| 20 |
|
| 21 |
+
# Chat Function
|
| 22 |
+
def chat_with_model(model_choice, user_message, chat_history, file=None):
|
| 23 |
+
if model_choice == "PDF Summarizer (T5)" and file is not None:
|
| 24 |
+
pdf_text = extract_text_from_pdf(file)
|
| 25 |
+
user_message += f"\n\nPDF Content:\n{pdf_text}"
|
| 26 |
+
|
| 27 |
+
if not user_message.strip():
|
| 28 |
+
return chat_history
|
| 29 |
|
|
|
|
|
|
|
| 30 |
model_info = models[model_choice]
|
| 31 |
tokenizer = model_info["tokenizer"]
|
| 32 |
model = model_info["model"]
|
| 33 |
|
| 34 |
+
# Tokenize Input
|
| 35 |
+
inputs = tokenizer(user_message, return_tensors="pt", padding=True, truncation=True, max_length=512)
|
| 36 |
+
# Generate Output
|
| 37 |
+
outputs = model.generate(**inputs, max_length=150, num_beams=5, early_stopping=True)
|
| 38 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 39 |
+
|
| 40 |
+
# Update Chat History
|
| 41 |
+
chat_history.append((user_message, response))
|
| 42 |
+
return chat_history
|
| 43 |
+
|
| 44 |
+
# Function to Extract Text from PDF
|
| 45 |
+
def extract_text_from_pdf(file):
|
| 46 |
+
from PyPDF2 import PdfReader
|
| 47 |
+
reader = PdfReader(file.name)
|
| 48 |
+
text = "\n".join(page.extract_text() for page in reader.pages if page.extract_text())
|
| 49 |
+
return text
|
| 50 |
+
|
| 51 |
+
# Interface Setup
|
| 52 |
+
def create_chat_interface():
|
| 53 |
+
with gr.Blocks(css="""
|
| 54 |
+
.chatbox {
|
| 55 |
+
background-color: #f7f7f8;
|
| 56 |
+
border-radius: 12px;
|
| 57 |
+
padding: 16px;
|
| 58 |
+
font-family: 'Segoe UI', Tahoma, sans-serif;
|
| 59 |
+
}
|
| 60 |
+
.chat-title {
|
| 61 |
+
font-size: 24px;
|
| 62 |
+
font-weight: bold;
|
| 63 |
+
text-align: center;
|
| 64 |
+
margin-bottom: 12px;
|
| 65 |
+
color: #3a9fd6;
|
| 66 |
+
}
|
| 67 |
+
""") as interface:
|
| 68 |
+
gr.Markdown("<div class='chat-title'>GPT-Style Chat Interface</div>")
|
| 69 |
+
|
| 70 |
+
with gr.Row():
|
| 71 |
+
model_choice = gr.Dropdown(
|
| 72 |
+
choices=list(models.keys()),
|
| 73 |
+
value="Text Generator (Bloom)",
|
| 74 |
+
label="Select Model"
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
chat_history = gr.Chatbot(label="Chat History", elem_classes="chatbox")
|
| 78 |
+
|
| 79 |
+
user_message = gr.Textbox(
|
| 80 |
+
placeholder="Type your message here...",
|
| 81 |
+
show_label=False,
|
| 82 |
+
elem_classes="chatbox",
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
file_input = gr.File(label="Upload PDF", visible=False, file_types=[".pdf"])
|
| 86 |
+
|
| 87 |
+
def toggle_pdf_input(selected_model):
|
| 88 |
+
return gr.update(visible=(selected_model == "PDF Summarizer (T5)"))
|
| 89 |
+
|
| 90 |
+
model_choice.change(fn=toggle_pdf_input, inputs=model_choice, outputs=file_input)
|
| 91 |
+
|
| 92 |
+
send_button = gr.Button("Send")
|
| 93 |
+
|
| 94 |
+
# Link the send button to the chat function
|
| 95 |
+
send_button.click(
|
| 96 |
+
chat_with_model,
|
| 97 |
+
inputs=[model_choice, user_message, chat_history, file_input],
|
| 98 |
+
outputs=chat_history,
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
return interface
|
| 102 |
+
|
| 103 |
+
if __name__ == "__main__":
|
| 104 |
+
interface = create_chat_interface()
|
| 105 |
+
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|