clemsadand
commited on
Commit
•
b475953
1
Parent(s):
b37710e
Create gradio_app.py
Browse files- gradio_app.py +27 -0
gradio_app.py
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from peft import PeftModel, PeftConfig
|
3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
4 |
+
|
5 |
+
config = PeftConfig.from_pretrained("clemsadand/quote_generator")
|
6 |
+
base_model = AutoModelForCausalLM.from_pretrained("gpt2")
|
7 |
+
model = PeftModel.from_pretrained(base_model, "clemsadand/quote_generator")
|
8 |
+
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained("gpt2")
|
10 |
+
|
11 |
+
def generate_quote(input_text):
|
12 |
+
input_tensor = tokenizer(input_text, return_tensors="pt")
|
13 |
+
output = model.generate(input_tensor["input_ids"], attention_mask=input_tensor["attention_mask"],
|
14 |
+
max_length=64, num_beams=5, no_repeat_ngram_size=2,
|
15 |
+
early_stopping=True, pad_token_id=tokenizer.eos_token_id, do_sample=True, temperature=0.7)
|
16 |
+
output = tokenizer.decode(output[0], ski_special_tokens=True, clean_up_tokenization_spaces=True)
|
17 |
+
return ".".join(output.split(".")[1:-1]).replace("\n", "").replace("->:", "") + "."
|
18 |
+
|
19 |
+
input_text = "Generate a quote about kindness with the keywords compassion, empathy, help, generosity, care"
|
20 |
+
# print(generate_quote(input_text))
|
21 |
+
|
22 |
+
input_textbox = gr.Textbox(label="Prompt", placeholder="Generate a quote about kindness with the keywords compassion, empathy, help, generosity, care", lines=2)
|
23 |
+
output_textbox = gr.Textbox(label="Generated quote", placeholder="", lines=4)
|
24 |
+
|
25 |
+
demo = gr.Interface(fn=generate_quote, inputs=input_textbox, outputs=output_textbox)
|
26 |
+
|
27 |
+
demo.launch()
|