kailouis commited on
Commit
b8c8b55
·
verified ·
1 Parent(s): 1b2727f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +127 -0
app.py ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import pipeline, set_seed, AutoTokenizer, AutoModelForSequenceClassification
3
+ from datasets import load_dataset
4
+ import gradio as gr
5
+ import requests
6
+ from bs4 import BeautifulSoup
7
+ from nltk.sentiment import SentimentIntensityAnalyzer
8
+ from flair.models import TextClassifier
9
+ from flair.data import Sentence
10
+ import newspaper3k
11
+
12
+ # Konfigurasi Model
13
+ set_seed(42)
14
+ nltk.download('vader_lexicon')
15
+
16
+ # Model Text Generation
17
+ generator = pipeline('text-generation', model='gpt2-xl') # Model lebih canggih
18
+
19
+ # Model Sentiment Analysis
20
+ sentiment_analyzer = SentimentIntensityAnalyzer()
21
+ classifier = TextClassifier.load('en-sentiment')
22
+
23
+ # Model Klasifikasi Topik
24
+ tokenizer_topic = AutoTokenizer.from_pretrained("facebook/bart-large-mnli")
25
+ model_topic = AutoModelForSequenceClassification.from_pretrained("facebook/bart-large-mnli")
26
+
27
+ # Fungsi Helper
28
+ def get_trending_topics(platform):
29
+ if platform == "Twitter":
30
+ # Scraping trending topics dari Twitter
31
+ url = "https://twitter.com/i/trends"
32
+ response = requests.get(url)
33
+ soup = BeautifulSoup(response.content, "html.parser")
34
+ trends = [trend.text.strip() for trend in soup.find_all("div", class_="css-901oao r-1awozwy r-18jsvk2 r-6koalj r-370sk r-a023e6 r-b88u0q r-rjixqe r-bcqeeo r-1udh08x r-3s2u2q r-qvutc0")]
35
+ return trends
36
+ elif platform == "TikTok":
37
+ # Scraping trending topics dari TikTok (perlu metode khusus)
38
+ # ...
39
+ return ["Trending TikTok 1", "Trending TikTok 2"]
40
+ else: # Instagram
41
+ # Scraping trending topics dari Instagram (perlu metode khusus)
42
+ # ...
43
+ return ["Trending Instagram 1", "Trending Instagram 2"]
44
+
45
+ def find_related_trend(topic, trends):
46
+ # Menggunakan model klasifikasi topik untuk mencari tren yang relevan
47
+ topic_sentence = Sentence(topic)
48
+ classifier.predict(topic_sentence)
49
+ topic_sentiment = topic_sentence.labels[0]
50
+
51
+ related_trends = []
52
+ for trend in trends:
53
+ trend_sentence = Sentence(trend)
54
+ classifier.predict(trend_sentence)
55
+ trend_sentiment = trend_sentence.labels[0]
56
+ if topic_sentiment.value == trend_sentiment.value:
57
+ related_trends.append(trend)
58
+ return related_trends
59
+
60
+ def make_clickbait_title(title):
61
+ # Menggunakan pola clickbait dan analisis sentimen
62
+ title = title.strip()
63
+ sentiment = sentiment_analyzer.polarity_scores(title)
64
+
65
+ if sentiment['compound'] > 0.5:
66
+ # Positif -> "Rahasia...", "Terungkap...", dll.
67
+ clickbait_phrases = ["Rahasia ", "Terungkap ", "Hebat! ", "Menakjubkan! "]
68
+ elif sentiment['compound'] < -0.5:
69
+ # Negatif -> "Mengerikan...", "Kontroversial...", dll.
70
+ clickbait_phrases = ["Mengerikan! ", "Kontroversial! ", "Awas! ", "Bahaya! "]
71
+ else:
72
+ # Netral -> "Kamu Tidak Akan Percaya...", "Heboh...", dll.
73
+ clickbait_phrases = ["Kamu Tidak Akan Percaya...", "Heboh! ", "Viral! ", "Trending! "]
74
+
75
+ return clickbait_phrases[0] + title
76
+
77
+ def analyze_content(content):
78
+ # Analisis sentimen dan topik
79
+ sentence = Sentence(content)
80
+ classifier.predict(sentence)
81
+ sentiment = sentence.labels[0]
82
+
83
+ # Klasifikasi topik (perlu pengembangan lebih lanjut)
84
+ inputs = tokenizer_topic(content, return_tensors="pt")
85
+ outputs = model_topic(**inputs)
86
+ topic_probs = torch.softmax(outputs.logits, dim=1)
87
+ # ... (Interpretasi hasil klasifikasi topik) ...
88
+ return sentiment, "Topik yang Diprediksi"
89
+
90
+ def generate_content(topic, format, tone, platform):
91
+ trending_topics = get_trending_topics(platform)
92
+ related_trends = find_related_trend(topic, trending_topics)
93
+
94
+ prompt = f"Buat konten {format} tentang {topic} dengan gaya {tone} yang "
95
+ if related_trends:
96
+ prompt += f"berkaitan dengan tren {', '.join(related_trends)}."
97
+ else:
98
+ prompt += f"berpotensi menjadi viral."
99
+
100
+ output = generator(prompt, max_length=500, num_return_sequences=1)
101
+ content = output[0]['generated_text']
102
+
103
+ if format == "Judul":
104
+ content = make_clickbait_title(content)
105
+
106
+ sentiment, predicted_topic = analyze_content(content)
107
+ return content, sentiment.value, predicted_topic
108
+
109
+ # Antarmuka Gradio
110
+ iface = gr.Interface(
111
+ fn=generate_content,
112
+ inputs=[
113
+ gr.Textbox(lines=2, placeholder="Masukkan topik..."),
114
+ gr.Dropdown(["Teks", "Judul", "Tweet", "Artikel"], label="Format Konten"),
115
+ gr.Dropdown(["Netral", "Provokatif", "Humor", "Marah", "Sedih"], label="Tone"),
116
+ gr.Dropdown(["Twitter", "TikTok", "Instagram"], label="Platform")
117
+ ],
118
+ outputs=[
119
+ "text",
120
+ "text",
121
+ "text"
122
+ ],
123
+ title="Pembuat Konten Viral (At Any Cost)",
124
+ description="Hasilkan konten yang dirancang untuk menjadi viral (Eksperimental)."
125
+ )
126
+
127
+ iface.launch(share=True) # share=True untuk mendapatkan link publik