Ozgur Unlu
commited on
Commit
•
7ae0911
1
Parent(s):
76c0903
changed ethical API
Browse files- app.py +118 -97
- requirements.txt +0 -1
app.py
CHANGED
@@ -5,7 +5,6 @@ import nltk
|
|
5 |
from datetime import datetime, timedelta
|
6 |
import requests
|
7 |
from bs4 import BeautifulSoup
|
8 |
-
import json
|
9 |
|
10 |
# Download required NLTK data
|
11 |
try:
|
@@ -15,28 +14,31 @@ except LookupError:
|
|
15 |
|
16 |
# Initialize models and tokenizers
|
17 |
def load_models():
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
36 |
|
37 |
# Simplified news fetching function
|
38 |
def fetch_recent_news(query, num_articles=3):
|
39 |
-
# Using Google News RSS feed
|
40 |
base_url = "https://news.google.com/rss/search"
|
41 |
params = {
|
42 |
'q': query,
|
@@ -76,7 +78,7 @@ def generate_content(
|
|
76 |
generator_tokenizer,
|
77 |
generator,
|
78 |
sentiment_analyzer,
|
79 |
-
|
80 |
):
|
81 |
# Format prompt based on platform
|
82 |
char_limit = 280 if platform == "Twitter" else 500
|
@@ -87,66 +89,71 @@ def generate_content(
|
|
87 |
|
88 |
# Create prompt
|
89 |
prompt = f"""
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
|
|
|
99 |
{news_context}
|
100 |
|
101 |
-
|
102 |
"""
|
103 |
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
generated_texts = [generator_tokenizer.decode(output, skip_special_tokens=True) for output in outputs]
|
116 |
-
|
117 |
-
# Filter and analyze content
|
118 |
-
filtered_content = []
|
119 |
-
for text in generated_texts:
|
120 |
-
# Clean up text
|
121 |
-
text = text.replace(prompt, "").strip()
|
122 |
|
123 |
-
|
124 |
-
if len(text) < 10:
|
125 |
-
continue
|
126 |
-
|
127 |
-
# Check sentiment
|
128 |
-
sentiment = sentiment_analyzer(text)[0]
|
129 |
|
130 |
-
#
|
131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
132 |
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
len(text) <= char_limit
|
138 |
-
):
|
139 |
-
filtered_content.append({
|
140 |
-
'text': text,
|
141 |
-
'sentiment': sentiment['label'],
|
142 |
-
'bias_score': f"{float(bias['score']):.2f}"
|
143 |
-
})
|
144 |
-
|
145 |
-
return filtered_content
|
146 |
|
147 |
# Gradio interface
|
148 |
def create_interface():
|
149 |
-
generator_tokenizer, generator, sentiment_analyzer,
|
150 |
|
151 |
def process_input(
|
152 |
product_name,
|
@@ -157,32 +164,35 @@ def create_interface():
|
|
157 |
platform,
|
158 |
tone
|
159 |
):
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
|
|
|
|
|
|
186 |
|
187 |
# Create the interface
|
188 |
iface = gr.Interface(
|
@@ -203,7 +213,18 @@ def create_interface():
|
|
203 |
outputs=gr.Textbox(label="Generated Content", lines=10),
|
204 |
title="Ethimar - AI Marketing Content Generator",
|
205 |
description="Generate ethical marketing content with AI-powered insights",
|
206 |
-
theme="default"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
207 |
)
|
208 |
|
209 |
return iface
|
|
|
5 |
from datetime import datetime, timedelta
|
6 |
import requests
|
7 |
from bs4 import BeautifulSoup
|
|
|
8 |
|
9 |
# Download required NLTK data
|
10 |
try:
|
|
|
14 |
|
15 |
# Initialize models and tokenizers
|
16 |
def load_models():
|
17 |
+
try:
|
18 |
+
# Text generation model
|
19 |
+
generator_model = "facebook/opt-350m"
|
20 |
+
generator_tokenizer = AutoTokenizer.from_pretrained(generator_model)
|
21 |
+
generator = AutoModelForCausalLM.from_pretrained(generator_model)
|
22 |
+
|
23 |
+
# Sentiment analysis
|
24 |
+
sentiment_analyzer = pipeline(
|
25 |
+
"sentiment-analysis",
|
26 |
+
model="finiteautomata/bertweet-base-sentiment-analysis"
|
27 |
+
)
|
28 |
+
|
29 |
+
# Content safety checker
|
30 |
+
content_checker = pipeline(
|
31 |
+
"text-classification",
|
32 |
+
model="facebook/roberta-hate-speech-dynabench-r4-target"
|
33 |
+
)
|
34 |
+
|
35 |
+
return generator_tokenizer, generator, sentiment_analyzer, content_checker
|
36 |
+
except Exception as e:
|
37 |
+
print(f"Error loading models: {str(e)}")
|
38 |
+
raise
|
39 |
|
40 |
# Simplified news fetching function
|
41 |
def fetch_recent_news(query, num_articles=3):
|
|
|
42 |
base_url = "https://news.google.com/rss/search"
|
43 |
params = {
|
44 |
'q': query,
|
|
|
78 |
generator_tokenizer,
|
79 |
generator,
|
80 |
sentiment_analyzer,
|
81 |
+
content_checker
|
82 |
):
|
83 |
# Format prompt based on platform
|
84 |
char_limit = 280 if platform == "Twitter" else 500
|
|
|
89 |
|
90 |
# Create prompt
|
91 |
prompt = f"""
|
92 |
+
Create a {platform} post with these requirements:
|
93 |
+
- Product Name: {product_name}
|
94 |
+
- Description: {product_description}
|
95 |
+
- Target Audience: {target_audience}
|
96 |
+
- Key Features: {key_features}
|
97 |
+
- Unique Benefits: {unique_benefits}
|
98 |
+
- Tone: {tone}
|
99 |
+
- Maximum Length: {char_limit} characters
|
100 |
|
101 |
+
Recent Market Context:
|
102 |
{news_context}
|
103 |
|
104 |
+
Generate a compelling {platform} post that highlights the product's benefits while maintaining a {tone} tone.
|
105 |
"""
|
106 |
|
107 |
+
try:
|
108 |
+
# Generate initial content
|
109 |
+
inputs = generator_tokenizer(prompt, return_tensors="pt", max_length=512, truncation=True)
|
110 |
+
outputs = generator.generate(
|
111 |
+
inputs["input_ids"],
|
112 |
+
max_length=char_limit + len(prompt),
|
113 |
+
num_return_sequences=3,
|
114 |
+
temperature=0.7,
|
115 |
+
top_p=0.9,
|
116 |
+
do_sample=True,
|
117 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
|
119 |
+
generated_texts = [generator_tokenizer.decode(output, skip_special_tokens=True) for output in outputs]
|
|
|
|
|
|
|
|
|
|
|
120 |
|
121 |
+
# Filter and analyze content
|
122 |
+
filtered_content = []
|
123 |
+
for text in generated_texts:
|
124 |
+
# Clean up text by removing the prompt
|
125 |
+
text = text.replace(prompt, "").strip()
|
126 |
+
|
127 |
+
# Skip if text is too short or too long
|
128 |
+
if len(text) < 10 or len(text) > char_limit:
|
129 |
+
continue
|
130 |
+
|
131 |
+
# Check sentiment
|
132 |
+
sentiment = sentiment_analyzer(text)[0]
|
133 |
+
|
134 |
+
# Check content safety
|
135 |
+
safety_check = content_checker(text)[0]
|
136 |
+
|
137 |
+
# Filter based on ethical considerations
|
138 |
+
if (
|
139 |
+
sentiment['label'] != 'negative' and
|
140 |
+
safety_check['label'] == 'not_hate' and
|
141 |
+
len(text) <= char_limit
|
142 |
+
):
|
143 |
+
filtered_content.append({
|
144 |
+
'text': text,
|
145 |
+
'sentiment': sentiment['label'],
|
146 |
+
'safety_score': f"{float(safety_check['score']):.2f}"
|
147 |
+
})
|
148 |
|
149 |
+
return filtered_content
|
150 |
+
except Exception as e:
|
151 |
+
print(f"Error generating content: {str(e)}")
|
152 |
+
return []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
153 |
|
154 |
# Gradio interface
|
155 |
def create_interface():
|
156 |
+
generator_tokenizer, generator, sentiment_analyzer, content_checker = load_models()
|
157 |
|
158 |
def process_input(
|
159 |
product_name,
|
|
|
164 |
platform,
|
165 |
tone
|
166 |
):
|
167 |
+
try:
|
168 |
+
results = generate_content(
|
169 |
+
product_name,
|
170 |
+
product_description,
|
171 |
+
target_audience,
|
172 |
+
key_features,
|
173 |
+
unique_benefits,
|
174 |
+
platform,
|
175 |
+
tone,
|
176 |
+
generator_tokenizer,
|
177 |
+
generator,
|
178 |
+
sentiment_analyzer,
|
179 |
+
content_checker
|
180 |
+
)
|
181 |
+
|
182 |
+
if not results:
|
183 |
+
return "No suitable content generated. Please try again with different parameters."
|
184 |
+
|
185 |
+
output = ""
|
186 |
+
for i, content in enumerate(results, 1):
|
187 |
+
output += f"\nVersion {i}:\n"
|
188 |
+
output += f"Content: {content['text']}\n"
|
189 |
+
output += f"Sentiment: {content['sentiment']}\n"
|
190 |
+
output += f"Safety Score: {content['safety_score']}\n"
|
191 |
+
output += "-" * 50 + "\n"
|
192 |
+
|
193 |
+
return output
|
194 |
+
except Exception as e:
|
195 |
+
return f"An error occurred: {str(e)}"
|
196 |
|
197 |
# Create the interface
|
198 |
iface = gr.Interface(
|
|
|
213 |
outputs=gr.Textbox(label="Generated Content", lines=10),
|
214 |
title="Ethimar - AI Marketing Content Generator",
|
215 |
description="Generate ethical marketing content with AI-powered insights",
|
216 |
+
theme="default",
|
217 |
+
examples=[
|
218 |
+
[
|
219 |
+
"EcoBottle",
|
220 |
+
"Sustainable water bottle made from recycled ocean plastic",
|
221 |
+
"Environmentally conscious young professionals",
|
222 |
+
"100% recycled materials, Insulated design, Leak-proof",
|
223 |
+
"Helps clean oceans, Keeps drinks cold for 24 hours",
|
224 |
+
"Twitter",
|
225 |
+
"professional"
|
226 |
+
]
|
227 |
+
]
|
228 |
)
|
229 |
|
230 |
return iface
|
requirements.txt
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
gradio
|
2 |
torch
|
3 |
transformers
|
4 |
-
newsapi-python
|
5 |
nltk
|
6 |
requests
|
7 |
beautifulsoup4
|
|
|
1 |
gradio
|
2 |
torch
|
3 |
transformers
|
|
|
4 |
nltk
|
5 |
requests
|
6 |
beautifulsoup4
|