Spaces:
Sleeping
Sleeping
File size: 4,253 Bytes
452611f f61d1a4 452611f 5c256b4 452611f 5c256b4 452611f 5c256b4 452611f 5c256b4 452611f 5c256b4 452611f 5c256b4 452611f 5c256b4 452611f f61d1a4 5c256b4 f61d1a4 5c256b4 f61d1a4 5c256b4 f61d1a4 5c256b4 452611f 5c256b4 452611f 5c256b4 452611f 5c256b4 452611f 0fd270a f61d1a4 5c256b4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
import gradio as gr
import os
import random
from tweet_analyzer import TweetDatasetProcessor
from dotenv import load_dotenv
load_dotenv()
class TwitterCloneApp:
def __init__(self):
self.processor = None
def process_upload(self, file):
"""Process uploaded PDF file and analyze personality."""
try:
if not file:
return "Error: No file uploaded. Please upload a PDF dataset."
self.processor = TweetDatasetProcessor()
text = self.processor.extract_text_from_pdf(file.name)
df = self.processor.process_pdf_content(text)
# Extract mentions and hashtags
mentions = df['mentions'].explode().dropna().unique().tolist()
hashtags = df['hashtags'].explode().dropna().unique().tolist()
# Perform personality analysis
personality_analysis = self.processor.analyze_personality()
# Format output
result = f"""
### Analysis Complete
- **Processed Tweets**: {len(df)}
- **Mentions**: {", ".join(mentions) if mentions else "None"}
- **Hashtags**: {", ".join(hashtags) if hashtags else "None"}
### Personality Analysis
{personality_analysis}
"""
return result
except Exception as e:
return f"Error processing file: {str(e)}"
def generate_tweet(self, context):
"""Generate a new tweet based on the analyzed personality."""
if not self.processor:
return "Error: Please upload and analyze a dataset first."
try:
# Predefined contexts
additional_contexts = [
"Comment on a recent technological advancement.",
"Share a motivational thought.",
"Discuss a current trending topic.",
"Reflect on a past experience.",
"Provide advice to followers."
]
# Extract historical topics
historical_topics = self.processor.analyze_topics(n_topics=5)
# Combine predefined contexts with historical topics
combined_contexts = additional_contexts + historical_topics
selected_contexts = random.sample(combined_contexts, min(3, len(combined_contexts)))
# Prioritize user context if provided
if context:
selected_contexts.insert(0, context)
# Generate the tweet
tweet = self.processor.generate_tweet(context=" | ".join(selected_contexts))
return f"### Generated Tweet\n{tweet}"
except Exception as e:
return f"Error generating tweet: {str(e)}"
def create_interface(self):
"""Create the Gradio interface."""
with gr.Blocks(title="Twitter Personality Cloner") as interface:
gr.Markdown("# Twitter Personality Cloner")
gr.Markdown("Upload a PDF file containing tweets to analyze the author's personality and generate new tweets in their style.")
with gr.Tab("Analyze Personality"):
file_input = gr.File(label="Upload PDF Dataset", file_types=[".pdf"])
analyze_button = gr.Button("Analyze Dataset")
analysis_output = gr.Textbox(label="Analysis Results", lines=10, interactive=False)
analyze_button.click(
fn=self.process_upload,
inputs=file_input,
outputs=analysis_output
)
with gr.Tab("Generate Tweets"):
context_input = gr.Textbox(label="Context (optional)", placeholder="Enter topic or context for the tweet")
generate_button = gr.Button("Generate Tweet")
tweet_output = gr.Textbox(label="Generated Tweet", lines=3, interactive=False)
generate_button.click(
fn=self.generate_tweet,
inputs=context_input,
outputs=tweet_output
)
return interface
def main():
app = TwitterCloneApp()
interface = app.create_interface()
interface.launch(share=True)
if __name__ == "__main__":
main()
|