NadaAljohani commited on
Commit
7140a92
1 Parent(s): 3039869

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -15
app.py CHANGED
@@ -1,41 +1,75 @@
1
  import gradio as gr
2
  from transformers import pipeline
 
3
 
4
- # Function to clean the output by truncating at the last full sentence
5
  def clean_output(text):
 
 
 
 
 
 
 
 
6
  if '.' in text:
7
- return text[:text.rfind('.')+1] # Truncate at the last full sentence
8
- return text # Return the text as is if no period is found
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  # Function to generate the story
11
- def generate_story(title, model_name):
12
  # Use text-generation pipeline from Hugging Face
13
  generator = pipeline('text-generation', model=model_name)
14
 
15
  # Generate the story based on the input title
16
  story = generator(title,
17
- max_length=300, # Set the maximum length for the generated text (story) to 230 tokens
18
- no_repeat_ngram_size=3, # Avoid repeating sequences of 3 words
19
- temperature=0.8, # Introduce some randomness for diversity
20
- top_p=0.95 # Nucleus sampling for more coherent text
21
  )[0]['generated_text']
22
 
23
- # Clean the generated story to ensure it ends with a full sentence
24
  cleaned_story = clean_output(story)
25
 
26
- # Return the cleaned story
27
  return cleaned_story
28
-
29
- # Gradio interface setup
30
  demo = gr.Interface(
31
  fn=generate_story,
32
  inputs=[
33
  gr.Textbox(label="Enter Story Title", placeholder="Type a title here..."), # Title input
34
- gr.Dropdown(choices=['gpt2', 'gpt2-large', 'EleutherAI/gpt-neo-2.7B','maldv/badger-writer-llama-3-8b', 'GOAT-AI/GOAT-70B-Storytelling'], value='gpt2', label="Select Model") # Model selection
 
 
 
 
 
 
 
 
35
  ],
36
  outputs="text",
37
  title="AI Story Generator",
38
- description="Generate a creative story using different AI models."
 
 
 
39
  )
40
 
41
- demo.launch(share=True)
 
 
1
  import gradio as gr
2
  from transformers import pipeline
3
+ import re
4
 
5
+ # Function to clean the output by truncating at the last full sentence and formatting paragraphs
6
  def clean_output(text):
7
+ # Remove unwanted symbols and replace them with appropriate punctuation or space
8
+ text = re.sub(r'\.{2,}', '.', text) # Replace sequences of more than one period with a single period
9
+ text = re.sub(r'[:\-]+', '', text) # Remove colons and dashes
10
+ text = re.sub(r'[()]+', '', text) # Remove parentheses
11
+ text = re.sub(r'\s+', ' ', text) # Replace excessive spaces
12
+ text = re.sub(r'[^\S\n]+', ' ', text) # Remove non-visible spaces like tabs
13
+
14
+ # Ensure the text ends with a full sentence
15
  if '.' in text:
16
+ text = text[:text.rfind('.')+1] # Truncate at the last full sentence
17
+
18
+ # Add paragraph breaks by splitting sentences into paragraphs
19
+ sentences = re.split(r'(?<!\w\.\w.)(?<![A-Z][a-z]\.)(?<=\.|\?)\s', text) # Split by sentence-ending punctuation
20
+
21
+ # Create paragraphs by grouping sentences
22
+ paragraph_size = len(sentences) // 4 # Split into approximately 4 paragraphs
23
+ paragraphs = [' '.join(sentences[i:i + paragraph_size]) for i in range(0, len(sentences), paragraph_size)]
24
+
25
+ # Limit the number of paragraphs to 3-4
26
+ paragraphs = paragraphs[:4]
27
+
28
+ # Join paragraphs with double line breaks
29
+ formatted_text = '\n\n'.join(paragraphs)
30
+
31
+ return formatted_text.strip() # Return trimmed and formatted text
32
 
33
  # Function to generate the story
34
+ def generate_story(title, model_name="gpt2"):
35
  # Use text-generation pipeline from Hugging Face
36
  generator = pipeline('text-generation', model=model_name)
37
 
38
  # Generate the story based on the input title
39
  story = generator(title,
40
+ max_length=500, # Set the maximum length for the generated text (story)
41
+ no_repeat_ngram_size=3, # Avoid repeating any sequence of 3 words (to prevent repetitive text)
42
+ temperature=0.8, # Introduce some randomness
43
+ top_p=0.95 # Use nucleus sampling for coherent output
44
  )[0]['generated_text']
45
 
46
+ # Clean the generated story to ensure it ends with a full sentence and trim it into paragraphs
47
  cleaned_story = clean_output(story)
48
 
49
+ # Return the cleaned and formatted story
50
  return cleaned_story
51
+ # Create the Gradio interface using gr.Interface
 
52
  demo = gr.Interface(
53
  fn=generate_story,
54
  inputs=[
55
  gr.Textbox(label="Enter Story Title", placeholder="Type a title here..."), # Title input
56
+ gr.Dropdown(choices=[
57
+ 'gpt2',
58
+ 'gpt2-large',
59
+ 'EleutherAI/gpt-neo-2.7B',
60
+ 'EleutherAI/gpt-j-6B',
61
+ 'maldv/badger-writer-llama-3-8b',
62
+ 'gpt-neo-2.7B'
63
+ 'TheBloke/Iambe-Storyteller-20B-GPTQ
64
+ ], value='gpt2', label="Choose Model") # Model selection
65
  ],
66
  outputs="text",
67
  title="AI Story Generator",
68
+ description="Generate a creative story using different AI models.",
69
+ examples=[
70
+ ["Sara burst into her friend's house, only to find it plunged into darkness. A strange, pulsing glow flickered from the corner, casting eerie shadows on the walls. Her heart raced as she called out, but there was no answer. Something wasn’t right. On the table sat an unfamiliar, glowing device—humming with energy. With a deep breath, Sara stepped closer, knowing that once she touched it, there would be no turning back."]
71
+ ]
72
  )
73
 
74
+ # Launch the interface with sharing enabled
75
+ demo.launch(share=True)