Manasa1 commited on
Commit
e90cbf4
1 Parent(s): 9d629bd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -19
app.py CHANGED
@@ -10,16 +10,6 @@ fine_tuned_tokenizer = GPT2Tokenizer.from_pretrained(model_dir)
10
  # Create a text-generation pipeline
11
  generator = pipeline('text-generation', model=fine_tuned_model, tokenizer=fine_tuned_tokenizer)
12
 
13
- # Function to dynamically truncate output while keeping it meaningful
14
- def truncate_tweet(tweet, max_length=280):
15
- # Ensure the tweet is concise, ending on a complete sentence
16
- if len(tweet) > max_length:
17
- tweet = tweet[:max_length]
18
- last_period = tweet.rfind(".")
19
- if last_period != -1:
20
- tweet = tweet[:last_period + 1]
21
- return tweet.strip()
22
-
23
  # Function to intelligently add relevant hashtags and emojis
24
  def add_relevant_tags(tweet, input_question):
25
  # Pre-defined mappings of topics to hashtags and emojis
@@ -54,20 +44,23 @@ def generate_tweet(input_question):
54
  # Format the input without "Question:" and "Answer:"
55
  prompt = input_question.strip()
56
 
57
- # Generate the output with a higher max_length for longer responses
58
  output = generator(prompt, max_length=300, num_return_sequences=1, temperature=0.7, top_p=0.9)
59
 
60
- # Extract the generated text and clean it
61
  tweet = output[0]['generated_text']
62
 
63
- # Remove "Question:" and "Answer:" from the generated text
64
- tweet = re.sub(r"(Question:|Answer:|A:|)", "", tweet).strip()
65
-
66
- # Remove any part of the tweet that starts with the input question
67
- tweet = tweet.replace(input_question, "").strip()
 
 
 
 
68
 
69
- # Truncate and add relevant hashtags and emojis
70
- tweet = truncate_tweet(tweet)
71
  tweet = add_relevant_tags(tweet, input_question)
72
 
73
  return tweet
 
10
  # Create a text-generation pipeline
11
  generator = pipeline('text-generation', model=fine_tuned_model, tokenizer=fine_tuned_tokenizer)
12
 
 
 
 
 
 
 
 
 
 
 
13
  # Function to intelligently add relevant hashtags and emojis
14
  def add_relevant_tags(tweet, input_question):
15
  # Pre-defined mappings of topics to hashtags and emojis
 
44
  # Format the input without "Question:" and "Answer:"
45
  prompt = input_question.strip()
46
 
47
+ # Generate the output with a max length of around 200 to 280 characters
48
  output = generator(prompt, max_length=300, num_return_sequences=1, temperature=0.7, top_p=0.9)
49
 
50
+ # Extract the generated text
51
  tweet = output[0]['generated_text']
52
 
53
+ # Ensure the tweet is between 200 and 280 characters
54
+ tweet_length = len(tweet)
55
+ if tweet_length > 280:
56
+ tweet = tweet[:280]
57
+ last_period = tweet.rfind(".")
58
+ if last_period != -1:
59
+ tweet = tweet[:last_period + 1]
60
+ elif tweet_length < 200:
61
+ tweet = tweet.ljust(200)
62
 
63
+ # Add relevant hashtags and emojis
 
64
  tweet = add_relevant_tags(tweet, input_question)
65
 
66
  return tweet