Update app.py
Browse files
app.py
CHANGED
|
@@ -1,60 +1,50 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import
|
| 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 |
-
text-align: center;
|
| 39 |
-
font-size: 12px;
|
| 40 |
-
color: #aaa;
|
| 41 |
-
margin-top: 30px;
|
| 42 |
-
}
|
| 43 |
-
""") as demo:
|
| 44 |
-
|
| 45 |
-
gr.Markdown("<div id='main-title'>π LLM for Content Generation</div>")
|
| 46 |
|
| 47 |
with gr.Row():
|
| 48 |
-
with gr.Column(
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
|
| 53 |
-
with gr.Column(
|
| 54 |
-
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
gr.Markdown("<div id='footer'>β¨ Powered by Hugging Face Transformers</div>")
|
| 59 |
|
| 60 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import (
|
| 3 |
+
GPT2LMHeadModel, GPT2Tokenizer,
|
| 4 |
+
AutoModelForCausalLM, AutoTokenizer
|
| 5 |
+
)
|
| 6 |
+
|
| 7 |
+
# Load Models
|
| 8 |
+
gpt2_model = GPT2LMHeadModel.from_pretrained("gpt2")
|
| 9 |
+
gpt2_tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
|
| 10 |
+
|
| 11 |
+
distilgpt2_model = GPT2LMHeadModel.from_pretrained("distilgpt2")
|
| 12 |
+
distilgpt2_tokenizer = GPT2Tokenizer.from_pretrained("distilgpt2")
|
| 13 |
+
|
| 14 |
+
bloom_model = AutoModelForCausalLM.from_pretrained("bigscience/bloom-560m")
|
| 15 |
+
bloom_tokenizer = AutoTokenizer.from_pretrained("bigscience/bloom-560m")
|
| 16 |
+
|
| 17 |
+
# Inference Function
|
| 18 |
+
def generate_text(prompt, model_name):
|
| 19 |
+
if model_name == "π§ GPT2":
|
| 20 |
+
inputs = gpt2_tokenizer.encode(prompt, return_tensors="pt")
|
| 21 |
+
output = gpt2_model.generate(inputs, max_length=100)
|
| 22 |
+
return gpt2_tokenizer.decode(output[0], skip_special_tokens=True)
|
| 23 |
+
|
| 24 |
+
elif model_name == "β‘ DistilGPT2":
|
| 25 |
+
inputs = distilgpt2_tokenizer.encode(prompt, return_tensors="pt")
|
| 26 |
+
output = distilgpt2_model.generate(inputs, max_length=100)
|
| 27 |
+
return distilgpt2_tokenizer.decode(output[0], skip_special_tokens=True)
|
| 28 |
+
|
| 29 |
+
elif model_name == "πΈ Bloom-560M":
|
| 30 |
+
inputs = bloom_tokenizer(prompt, return_tensors="pt")
|
| 31 |
+
output = bloom_model.generate(inputs["input_ids"], max_length=100)
|
| 32 |
+
return bloom_tokenizer.decode(output[0], skip_special_tokens=True)
|
| 33 |
+
|
| 34 |
+
# Gradio UI
|
| 35 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 36 |
+
gr.Markdown("<h1 style='text-align: center; color: brown;'>LLM for Content Generation</h1>")
|
| 37 |
+
gr.Markdown("<div style='text-align: center;'>Generate high-quality text using three powerful LLMs</div>")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
with gr.Row():
|
| 40 |
+
with gr.Column():
|
| 41 |
+
prompt = gr.Textbox(label="Enter a topic or prompt")
|
| 42 |
+
model_choice = gr.Radio(["π§ GPT2", "β‘ DistilGPT2", "πΈ Bloom-560M"], label="Choose a Model")
|
| 43 |
+
submit = gr.Button("Generate")
|
| 44 |
|
| 45 |
+
with gr.Column():
|
| 46 |
+
output = gr.Textbox(label="Generated Text", lines=10)
|
| 47 |
|
| 48 |
+
submit.click(fn=generate_text, inputs=[prompt, model_choice], outputs=output)
|
|
|
|
|
|
|
| 49 |
|
| 50 |
demo.launch()
|