Update app.py
Browse files
app.py
CHANGED
|
@@ -1,51 +1,55 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
-
from
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# Replace with your model name
|
| 6 |
-
|
| 7 |
#MODEL_NAME = "unsloth/gemma-7b-bnb-4bit"
|
| 8 |
-
MODEL_NAME = "
|
| 9 |
-
#MODEL_NAME = "unsloth/mistral-7b-bnb-4bit"
|
| 10 |
|
| 11 |
# Load the model and tokenizer
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
torch_dtype=torch.float16,
|
| 16 |
-
load_in_4bit=True, # Load the model in 4-bit precision
|
| 17 |
-
# Removed the unsupported argument
|
| 18 |
-
)
|
| 19 |
-
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
#
|
| 23 |
-
|
| 24 |
-
# Create a pipeline for text generation
|
| 25 |
-
generator = pipeline(
|
| 26 |
-
#task="text-generation",
|
| 27 |
-
task="summarization",
|
| 28 |
-
model=model,
|
| 29 |
-
tokenizer=tokenizer,
|
| 30 |
-
max_new_tokens=50, # Adjust as needed
|
| 31 |
-
do_sample=True,
|
| 32 |
-
top_k=10,
|
| 33 |
-
num_return_sequences=1,
|
| 34 |
-
eos_token_id=tokenizer.eos_token_id,
|
| 35 |
-
)
|
| 36 |
-
|
| 37 |
-
def generate_text(email):
|
| 38 |
-
result = generator("Generate a subject line for the following email.\n"+email)
|
| 39 |
-
return result[0]["generated_text"]
|
| 40 |
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
demo = gr.Interface(
|
| 44 |
-
fn=
|
| 45 |
-
inputs=gr.Textbox(lines=
|
| 46 |
-
outputs=gr.Textbox(label="Generated Subject")
|
| 47 |
-
title="Email Subject Generation demo",
|
| 48 |
-
description="Enter an email and let the model generate the subject for you!",
|
| 49 |
)
|
| 50 |
|
| 51 |
-
demo.launch(
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
+
from unsloth import FastLanguageModel
|
| 4 |
+
from transformers import TextStreamer
|
| 5 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 6 |
|
| 7 |
# Replace with your model name
|
| 8 |
+
MODEL_NAME = "ssirikon/Gemma7b-bnb-Unsloth"
|
| 9 |
#MODEL_NAME = "unsloth/gemma-7b-bnb-4bit"
|
| 10 |
+
#MODEL_NAME = "Lohith9459/gemma7b"
|
|
|
|
| 11 |
|
| 12 |
# Load the model and tokenizer
|
| 13 |
+
max_seq_length = 512
|
| 14 |
+
dtype = torch.bfloat16
|
| 15 |
+
load_in_4bit = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
#model = FastLanguageModel.from_pretrained(MODEL_NAME, max_seq_length=max_seq_length, dtype=dtype, load_in_4bit=load_in_4bit)
|
| 18 |
+
#tokenizer = model.tokenizer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, torch_dtype=torch.bfloat16, device_map="auto")
|
| 21 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 22 |
|
| 23 |
+
def generate_subject(email_body):
|
| 24 |
+
instruction = "Generate a subject line for the following email."
|
| 25 |
+
formatted_text = f"""Below is an instruction that describes a task. \
|
| 26 |
+
Write a response that appropriately completes the request.
|
| 27 |
+
### Instruction:
|
| 28 |
+
{instruction}
|
| 29 |
+
### Input:
|
| 30 |
+
{email_body}
|
| 31 |
+
### Response:
|
| 32 |
+
"""
|
| 33 |
+
inputs = tokenizer([formatted_text], return_tensors="pt").to("cuda")
|
| 34 |
+
text_streamer = TextStreamer(tokenizer)
|
| 35 |
+
generated_ids = model.generate(**inputs, streamer=text_streamer, max_new_tokens=512)
|
| 36 |
+
generated_text = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
|
| 37 |
+
|
| 38 |
+
def extract_subject(text):
|
| 39 |
+
start_tag = "### Response:"
|
| 40 |
+
start_idx = text.find(start_tag)
|
| 41 |
+
if start_idx == -1:
|
| 42 |
+
return None
|
| 43 |
+
subject = text[start_idx + len(start_tag):].strip()
|
| 44 |
+
return subject
|
| 45 |
+
|
| 46 |
+
return extract_subject(generated_text)
|
| 47 |
+
|
| 48 |
+
# Create the Gradio interface
|
| 49 |
demo = gr.Interface(
|
| 50 |
+
fn=generate_subject,
|
| 51 |
+
inputs=gr.Textbox(lines=20, label="Email Body"),
|
| 52 |
+
outputs=gr.Textbox(label="Generated Subject")
|
|
|
|
|
|
|
| 53 |
)
|
| 54 |
|
| 55 |
+
demo.launch()
|