Spaces:
Sleeping
Sleeping
Upload app_fixed.py
Browse files- app_fixed.py +101 -0
app_fixed.py
ADDED
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import gradio as gr
|
3 |
+
import os
|
4 |
+
from tweet_analyzer import TweetDatasetProcessor
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
|
7 |
+
load_dotenv()
|
8 |
+
|
9 |
+
class TwitterCloneApp:
|
10 |
+
def __init__(self):
|
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 analyze_topics(self, n_topics=5):
|
30 |
+
"""Analyze topics in the dataset"""
|
31 |
+
if not self.processor:
|
32 |
+
return "Please upload and analyze a dataset first."
|
33 |
+
|
34 |
+
# Check if the analyze_topics method exists
|
35 |
+
if hasattr(self.processor, 'analyze_topics'):
|
36 |
+
try:
|
37 |
+
topics = self.processor.analyze_topics(n_topics=n_topics)
|
38 |
+
return f"Identified Topics:\n{topics}"
|
39 |
+
except Exception as e:
|
40 |
+
return f"Error analyzing topics: {str(e)}"
|
41 |
+
else:
|
42 |
+
return "Topic analysis is not available in this version of TweetDatasetProcessor."
|
43 |
+
|
44 |
+
def generate_tweet(self, context):
|
45 |
+
"""Generate a new tweet based on the analyzed personality"""
|
46 |
+
if not self.processor:
|
47 |
+
return "Please upload and analyze a dataset first."
|
48 |
+
|
49 |
+
try:
|
50 |
+
tweet = self.processor.generate_tweet(context)
|
51 |
+
return tweet
|
52 |
+
except Exception as e:
|
53 |
+
return f"Error generating tweet: {str(e)}"
|
54 |
+
|
55 |
+
def create_interface(self):
|
56 |
+
"""Create the Gradio interface"""
|
57 |
+
with gr.Blocks(title="Twitter Personality Cloner") as interface:
|
58 |
+
gr.Markdown("# Twitter Personality Cloner")
|
59 |
+
gr.Markdown("Upload a PDF file containing tweets to analyze the author's personality and generate new tweets in their style.")
|
60 |
+
|
61 |
+
with gr.Tab("Analyze Personality"):
|
62 |
+
file_input = gr.File(label="Upload PDF Dataset", file_types=[".pdf"])
|
63 |
+
analyze_button = gr.Button("Analyze Dataset")
|
64 |
+
analysis_output = gr.Textbox(label="Analysis Results", lines=10)
|
65 |
+
|
66 |
+
analyze_button.click(
|
67 |
+
fn=self.process_upload,
|
68 |
+
inputs=file_input,
|
69 |
+
outputs=analysis_output
|
70 |
+
)
|
71 |
+
|
72 |
+
with gr.Tab("Analyze Topics"):
|
73 |
+
topic_button = gr.Button("Analyze Topics")
|
74 |
+
topic_output = gr.Textbox(label="Identified Topics")
|
75 |
+
|
76 |
+
topic_button.click(
|
77 |
+
fn=self.analyze_topics,
|
78 |
+
inputs=[],
|
79 |
+
outputs=topic_output
|
80 |
+
)
|
81 |
+
|
82 |
+
with gr.Tab("Generate Tweets"):
|
83 |
+
context_input = gr.Textbox(label="Context (optional)", placeholder="Enter topic or context for the tweet")
|
84 |
+
generate_button = gr.Button("Generate Tweet")
|
85 |
+
tweet_output = gr.Textbox(label="Generated Tweet")
|
86 |
+
|
87 |
+
generate_button.click(
|
88 |
+
fn=self.generate_tweet,
|
89 |
+
inputs=context_input,
|
90 |
+
outputs=tweet_output
|
91 |
+
)
|
92 |
+
|
93 |
+
return interface
|
94 |
+
|
95 |
+
def main():
|
96 |
+
app = TwitterCloneApp()
|
97 |
+
interface = app.create_interface()
|
98 |
+
interface.launch(share=True)
|
99 |
+
|
100 |
+
if __name__ == "__main__":
|
101 |
+
main()
|