Manasa1 commited on
Commit
5c256b4
·
verified ·
1 Parent(s): 3f9241e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -12
app.py CHANGED
@@ -11,25 +11,40 @@ class TwitterCloneApp:
11
  self.processor = None
12
 
13
  def process_upload(self, file):
14
- """Process uploaded PDF file and analyze personality"""
15
  try:
 
 
 
16
  self.processor = TweetDatasetProcessor()
17
  text = self.processor.extract_text_from_pdf(file.name)
18
  df = self.processor.process_pdf_content(text)
19
-
 
20
  mentions = df['mentions'].explode().dropna().unique().tolist()
21
  hashtags = df['hashtags'].explode().dropna().unique().tolist()
22
 
 
23
  personality_analysis = self.processor.analyze_personality()
24
 
25
- return f"Processed {len(df)} tweets\n\nMentions: {mentions}\n\nHashtags: {hashtags}\n\nPersonality Analysis:\n{personality_analysis}"
 
 
 
 
 
 
 
 
 
 
26
  except Exception as e:
27
  return f"Error processing file: {str(e)}"
28
 
29
  def generate_tweet(self, context):
30
- """Generate a new tweet based on the analyzed personality"""
31
  if not self.processor:
32
- return "Please upload and analyze a dataset first."
33
 
34
  try:
35
  # Predefined contexts
@@ -48,18 +63,18 @@ class TwitterCloneApp:
48
  combined_contexts = additional_contexts + historical_topics
49
  selected_contexts = random.sample(combined_contexts, min(3, len(combined_contexts)))
50
 
51
- # If user provided context, add it to the mix
52
  if context:
53
- selected_contexts.append(context)
54
 
55
- # Generate the tweet with diverse contexts
56
  tweet = self.processor.generate_tweet(context=" | ".join(selected_contexts))
57
- return tweet
58
  except Exception as e:
59
  return f"Error generating tweet: {str(e)}"
60
 
61
  def create_interface(self):
62
- """Create the Gradio interface"""
63
  with gr.Blocks(title="Twitter Personality Cloner") as interface:
64
  gr.Markdown("# Twitter Personality Cloner")
65
  gr.Markdown("Upload a PDF file containing tweets to analyze the author's personality and generate new tweets in their style.")
@@ -67,7 +82,7 @@ class TwitterCloneApp:
67
  with gr.Tab("Analyze Personality"):
68
  file_input = gr.File(label="Upload PDF Dataset", file_types=[".pdf"])
69
  analyze_button = gr.Button("Analyze Dataset")
70
- analysis_output = gr.Textbox(label="Analysis Results", lines=10)
71
 
72
  analyze_button.click(
73
  fn=self.process_upload,
@@ -78,7 +93,7 @@ class TwitterCloneApp:
78
  with gr.Tab("Generate Tweets"):
79
  context_input = gr.Textbox(label="Context (optional)", placeholder="Enter topic or context for the tweet")
80
  generate_button = gr.Button("Generate Tweet")
81
- tweet_output = gr.Textbox(label="Generated Tweet")
82
 
83
  generate_button.click(
84
  fn=self.generate_tweet,
@@ -97,3 +112,4 @@ if __name__ == "__main__":
97
  main()
98
 
99
 
 
 
11
  self.processor = None
12
 
13
  def process_upload(self, file):
14
+ """Process uploaded PDF file and analyze personality."""
15
  try:
16
+ if not file:
17
+ return "Error: No file uploaded. Please upload a PDF dataset."
18
+
19
  self.processor = TweetDatasetProcessor()
20
  text = self.processor.extract_text_from_pdf(file.name)
21
  df = self.processor.process_pdf_content(text)
22
+
23
+ # Extract mentions and hashtags
24
  mentions = df['mentions'].explode().dropna().unique().tolist()
25
  hashtags = df['hashtags'].explode().dropna().unique().tolist()
26
 
27
+ # Perform personality analysis
28
  personality_analysis = self.processor.analyze_personality()
29
 
30
+ # Format output
31
+ result = f"""
32
+ ### Analysis Complete
33
+ - **Processed Tweets**: {len(df)}
34
+ - **Mentions**: {", ".join(mentions) if mentions else "None"}
35
+ - **Hashtags**: {", ".join(hashtags) if hashtags else "None"}
36
+
37
+ ### Personality Analysis
38
+ {personality_analysis}
39
+ """
40
+ return result
41
  except Exception as e:
42
  return f"Error processing file: {str(e)}"
43
 
44
  def generate_tweet(self, context):
45
+ """Generate a new tweet based on the analyzed personality."""
46
  if not self.processor:
47
+ return "Error: Please upload and analyze a dataset first."
48
 
49
  try:
50
  # Predefined contexts
 
63
  combined_contexts = additional_contexts + historical_topics
64
  selected_contexts = random.sample(combined_contexts, min(3, len(combined_contexts)))
65
 
66
+ # Prioritize user context if provided
67
  if context:
68
+ selected_contexts.insert(0, context)
69
 
70
+ # Generate the tweet
71
  tweet = self.processor.generate_tweet(context=" | ".join(selected_contexts))
72
+ return f"### Generated Tweet\n{tweet}"
73
  except Exception as e:
74
  return f"Error generating tweet: {str(e)}"
75
 
76
  def create_interface(self):
77
+ """Create the Gradio interface."""
78
  with gr.Blocks(title="Twitter Personality Cloner") as interface:
79
  gr.Markdown("# Twitter Personality Cloner")
80
  gr.Markdown("Upload a PDF file containing tweets to analyze the author's personality and generate new tweets in their style.")
 
82
  with gr.Tab("Analyze Personality"):
83
  file_input = gr.File(label="Upload PDF Dataset", file_types=[".pdf"])
84
  analyze_button = gr.Button("Analyze Dataset")
85
+ analysis_output = gr.Textbox(label="Analysis Results", lines=10, interactive=False)
86
 
87
  analyze_button.click(
88
  fn=self.process_upload,
 
93
  with gr.Tab("Generate Tweets"):
94
  context_input = gr.Textbox(label="Context (optional)", placeholder="Enter topic or context for the tweet")
95
  generate_button = gr.Button("Generate Tweet")
96
+ tweet_output = gr.Textbox(label="Generated Tweet", lines=3, interactive=False)
97
 
98
  generate_button.click(
99
  fn=self.generate_tweet,
 
112
  main()
113
 
114
 
115
+