NadaAljohani commited on
Commit
8a11fad
1 Parent(s): f22752e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -24
app.py CHANGED
@@ -5,31 +5,15 @@ from transformers import pipeline
5
  def generate_story(title, model_name):
6
  # Use text-generation pipeline from Hugging Face
7
  generator = pipeline('text-generation', model=model_name)
8
-
9
  # Generate the story based on the input title
10
- story = generator(
11
- title,
12
- max_length=500, # Increase the length of the generated story to 500 tokens
13
- no_repeat_ngram_size=3, # Avoid repeating sequences of 3 words
14
- temperature=0.7, # Decrease temperature for more coherent, less random text
15
- top_p=0.9, # Consider top 90% probable words for diversity
16
- top_k=40, # Limit the next token choices to the top 40
17
- repetition_penalty=1.1, # Penalize repetition slightly to improve fluency
18
- do_sample=True # Enable sampling for creativity
19
- )[0]['generated_text']
20
-
21
- # Clean the generated story to ensure it ends with a complete sentence
22
- cleaned_story = clean_output(story)
23
-
24
- # Return the cleaned story
25
- return cleaned_story
26
-
27
- # Function to clean the output by truncating at the last full sentence
28
- def clean_output(text):
29
- if '.' in text:
30
- return text[:text.rfind('.')+1] # Truncate at the last full sentence
31
- return text # Return the text as is if no period is found
32
-
33
 
34
  # Create the Gradio interface using gr.Interface
35
  demo = gr.Interface(
 
5
  def generate_story(title, model_name):
6
  # Use text-generation pipeline from Hugging Face
7
  generator = pipeline('text-generation', model=model_name)
 
8
  # Generate the story based on the input title
9
+ story = generator(title,
10
+ max_length=230, # Set the maximum length for the generated text (story) to 230 tokens
11
+ no_repeat_ngram_size=3, # Avoid repeating any sequence of 3 words (to prevent repetitive text)
12
+ temperature=0.8, # Introduce some randomness; higher values make the output more random, lower makes it more deterministic
13
+ top_p=0.95 # Use nucleus sampling (top-p sampling) to focus on the top 95% of probable words, making the text more coherent
14
+ )
15
+ # Return the generated text
16
+ return story[0]['generated_text']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
  # Create the Gradio interface using gr.Interface
19
  demo = gr.Interface(