app.py
CHANGED
|
@@ -1,7 +1,42 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 2 |
import gradio as gr
|
| 3 |
+
import spaces
|
| 4 |
|
| 5 |
+
# Load the pre-trained model and tokenizer
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained("merve/chatgpt-prompts-bart-long")
|
| 7 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("merve/chatgpt-prompts-bart-long", from_tf=True)
|
| 8 |
|
| 9 |
+
@spaces.GPU
|
| 10 |
+
def generate(prompt):
|
| 11 |
+
# Tokenize the input prompt
|
| 12 |
+
batch = tokenizer(prompt, return_tensors="pt")
|
| 13 |
+
|
| 14 |
+
# Generate response using the model
|
| 15 |
+
generated_ids = model.generate(batch["input_ids"], max_new_tokens=150)
|
| 16 |
+
|
| 17 |
+
# Decode the output into text
|
| 18 |
+
output = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)
|
| 19 |
+
return output[0]
|
| 20 |
+
|
| 21 |
+
# Gradio input component to accept character details
|
| 22 |
+
input_component = gr.Textbox(label="Input a persona or character role, e.g., warrior, scientist", value="warrior")
|
| 23 |
+
output_component = gr.Textbox(label="Generated Character Description")
|
| 24 |
+
|
| 25 |
+
# Example character roles for the user to select from
|
| 26 |
+
examples = [["warrior"], ["scientist"], ["mage"], ["detective"], ["robotic engineer"]]
|
| 27 |
+
|
| 28 |
+
# Description for the Gradio interface
|
| 29 |
+
description = (
|
| 30 |
+
"This app generates a character description based on a persona. "
|
| 31 |
+
"You can input a specific role or persona, such as 'warrior' or 'scientist', and the AI will generate a detailed description."
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
# Create the Gradio interface
|
| 35 |
+
gr.Interface(
|
| 36 |
+
generate,
|
| 37 |
+
inputs=input_component,
|
| 38 |
+
outputs=output_component,
|
| 39 |
+
examples=examples,
|
| 40 |
+
title="🧙 Character Creator using AI 🧙",
|
| 41 |
+
description=description
|
| 42 |
+
).launch()
|