Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -44,41 +44,43 @@ def translate_to_chinese(text):
|
|
| 44 |
return translation
|
| 45 |
|
| 46 |
|
| 47 |
-
# text2story - using
|
| 48 |
def text2story(text):
|
| 49 |
try:
|
| 50 |
-
|
| 51 |
-
generator = pipeline("text-generation", model="2173ars/llama-3-8b-Instruct-bnb-4bit-personal-shortstory")
|
| 52 |
-
|
| 53 |
-
# Create a prompt for the story
|
| 54 |
prompt = f"{text}"
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
| 68 |
except Exception as e:
|
| 69 |
# Fallback to simpler model if the advanced one fails
|
| 70 |
-
fallback_generator =
|
| 71 |
-
fallback_prompt = f"{text}
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
def load_css(css_file):
|
| 84 |
with open(css_file) as f:
|
|
|
|
| 44 |
return translation
|
| 45 |
|
| 46 |
|
| 47 |
+
# text2story - using llama-3-8b-Instruct-bnb-4bit-personal-shortstory model for better stories
|
| 48 |
def text2story(text):
|
| 49 |
try:
|
| 50 |
+
model = AutoModel.from_pretrained("2173ars/llama-3-8b-Instruct-bnb-4bit-personal-shortstory")
|
|
|
|
|
|
|
|
|
|
| 51 |
prompt = f"{text}"
|
| 52 |
+
tokenizer = AutoTokenizer.from_pretrained("2173ars/llama-3-8b-Instruct-bnb-4bit-personal-shortstory")
|
| 53 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 54 |
+
outputs = model.generate(
|
| 55 |
+
inputs.input_ids,
|
| 56 |
+
max_new_tokens=250,
|
| 57 |
+
temperature=0.7,
|
| 58 |
+
top_p=0.9,
|
| 59 |
+
top_k=40,
|
| 60 |
+
repetition_penalty=1.2,
|
| 61 |
+
do_sample=True,
|
| 62 |
+
pad_token_id=tokenizer.eos_token_id
|
| 63 |
+
)
|
| 64 |
+
story = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 65 |
+
return story
|
| 66 |
except Exception as e:
|
| 67 |
# Fallback to simpler model if the advanced one fails
|
| 68 |
+
fallback_generator = AutoModel.from_pretrained("openai-community/gpt2")
|
| 69 |
+
fallback_prompt = f"{text}"
|
| 70 |
+
tokenizer = AutoTokenizer.from_pretrained("openai-community/gpt2")
|
| 71 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 72 |
+
fallback_story = model.generate(
|
| 73 |
+
inputs.input_ids,
|
| 74 |
+
max_new_tokens=250,
|
| 75 |
+
temperature=0.7,
|
| 76 |
+
top_p=0.9,
|
| 77 |
+
top_k=40,
|
| 78 |
+
repetition_penalty=1.2,
|
| 79 |
+
do_sample=True,
|
| 80 |
+
pad_token_id=tokenizer.eos_token_id
|
| 81 |
+
)
|
| 82 |
+
fallback_story = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 83 |
+
return fallback_story
|
| 84 |
|
| 85 |
def load_css(css_file):
|
| 86 |
with open(css_file) as f:
|