YixuanWang commited on
Commit
6fc36a2
·
verified ·
1 Parent(s): f3cf000

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -23
app.py CHANGED
@@ -133,16 +133,18 @@ class RecommendationSystem:
133
  def create_gradio_interface(recommendation_system: RecommendationSystem) -> gr.Interface:
134
  with gr.Blocks(theme=gr.themes.Soft()) as interface:
135
  gr.Markdown("""
136
- # 推文推荐系统
137
- 调整权重以获取个性化推荐
 
 
138
  """)
139
 
140
  with gr.Row():
141
  with gr.Column(scale=1):
142
- visibility_weight = gr.Slider(0, 1, 0.5, label="可信度权重", info="调整对内容可信度的重视程度")
143
- sentiment_weight = gr.Slider(0, 1, 0.3, label="情感倾向权重", info="调整对情感倾向的重视程度")
144
- popularity_weight = gr.Slider(0, 1, 0.2, label="热度权重", info="调整对内容热度的重视程度")
145
- submit_btn = gr.Button("获取推荐", variant="primary")
146
 
147
  with gr.Column(scale=2):
148
  output_html = gr.HTML()
@@ -150,39 +152,37 @@ def create_gradio_interface(recommendation_system: RecommendationSystem) -> gr.I
150
  def format_recommendations(raw_recommendations):
151
  html = '<div style="font-family: sans-serif;">'
152
 
153
- # 添加评分说明
154
  html += '''
155
  <div style="margin-bottom: 20px; padding: 15px; background-color: #f5f5f5; border-radius: 8px;">
156
- <h3 style="margin-top: 0;">评分说明</h3>
157
  <ul style="margin: 0;">
158
- <li><strong>可信度</strong>:内容的可信程度评估</li>
159
- <li><strong>情感倾向</strong>:文本的情感分析(积极/消极/中性)</li>
160
- <li><strong>热度</strong>:基于点赞和转发的归一化分数</li>
161
  </ul>
162
  </div>
163
  '''
164
 
165
- # 显示推荐的tweets
166
  for i, rec in enumerate(raw_recommendations["recommendations"], 1):
167
  scores = rec["scores"]
168
  html += f'''
169
  <div style="margin-bottom: 15px; padding: 15px; border: 1px solid #ddd; border-radius: 8px;">
170
  <div style="margin-bottom: 10px; font-size: 1.1em;">{rec["text"]}</div>
171
- <div style="display: flex; flex-wrap: wrap; gap: 10px; font-size: 0.9em; color: #666;">
172
- <span style="padding: 3px 8px; background-color: #e3f2fd; border-radius: 4px;">
173
- 总分: {scores["总分"]}
174
  </span>
175
- <span style="padding: 3px 8px; background-color: #e8f5e9; border-radius: 4px;">
176
- 可信度: {scores["可信度"]}
177
  </span>
178
- <span style="padding: 3px 8px; background-color: #fff3e0; border-radius: 4px;">
179
- 情感: {scores["情感倾向"]}
180
  </span>
181
- <span style="padding: 3px 8px; background-color: #fce4ec; border-radius: 4px;">
182
- 热度: {scores["热度"]}
183
  </span>
184
- <span style="padding: 3px 8px; background-color: #f3e5f5; border-radius: 4px;">
185
- {scores["互动数"]}
186
  </span>
187
  </div>
188
  </div>
@@ -200,6 +200,44 @@ def create_gradio_interface(recommendation_system: RecommendationSystem) -> gr.I
200
 
201
  return interface
202
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
203
  def main():
204
  try:
205
  recommendation_system = RecommendationSystem(
 
133
  def create_gradio_interface(recommendation_system: RecommendationSystem) -> gr.Interface:
134
  with gr.Blocks(theme=gr.themes.Soft()) as interface:
135
  gr.Markdown("""
136
+ # Tweet Recommendation System
137
+ Adjust weights to get personalized recommendations
138
+
139
+ Note: To protect user privacy, some tweet content has been redacted or anonymized.
140
  """)
141
 
142
  with gr.Row():
143
  with gr.Column(scale=1):
144
+ visibility_weight = gr.Slider(0, 1, 0.5, label="Credibility Weight", info="Adjust importance of content credibility")
145
+ sentiment_weight = gr.Slider(0, 1, 0.3, label="Sentiment Weight", info="Adjust importance of emotional tone")
146
+ popularity_weight = gr.Slider(0, 1, 0.2, label="Popularity Weight", info="Adjust importance of engagement metrics")
147
+ submit_btn = gr.Button("Get Recommendations", variant="primary")
148
 
149
  with gr.Column(scale=2):
150
  output_html = gr.HTML()
 
152
  def format_recommendations(raw_recommendations):
153
  html = '<div style="font-family: sans-serif;">'
154
 
 
155
  html += '''
156
  <div style="margin-bottom: 20px; padding: 15px; background-color: #f5f5f5; border-radius: 8px;">
157
+ <h3 style="margin-top: 0;">Score Guide</h3>
158
  <ul style="margin: 0;">
159
+ <li><strong>Credibility</strong>: Assessment of content reliability</li>
160
+ <li><strong>Sentiment</strong>: Text emotional analysis (Positive/Negative/Neutral)</li>
161
+ <li><strong>Popularity</strong>: Normalized score based on likes and retweets</li>
162
  </ul>
163
  </div>
164
  '''
165
 
 
166
  for i, rec in enumerate(raw_recommendations["recommendations"], 1):
167
  scores = rec["scores"]
168
  html += f'''
169
  <div style="margin-bottom: 15px; padding: 15px; border: 1px solid #ddd; border-radius: 8px;">
170
  <div style="margin-bottom: 10px; font-size: 1.1em;">{rec["text"]}</div>
171
+ <div style="display: flex; flex-wrap: wrap; gap: 10px; font-size: 0.9em;">
172
+ <span style="padding: 3px 8px; background-color: #1976d2; color: white; border-radius: 4px;">
173
+ Score: {scores["总分"]}
174
  </span>
175
+ <span style="padding: 3px 8px; background-color: #2e7d32; color: white; border-radius: 4px;">
176
+ Credibility: {scores["可信度"]}
177
  </span>
178
+ <span style="padding: 3px 8px; background-color: #ed6c02; color: white; border-radius: 4px;">
179
+ Sentiment: {scores["情感倾向"]}
180
  </span>
181
+ <span style="padding: 3px 8px; background-color: #d32f2f; color: white; border-radius: 4px;">
182
+ Popularity: {scores["热度"]}
183
  </span>
184
+ <span style="padding: 3px 8px; background-color: #7b1fa2; color: white; border-radius: 4px;">
185
+ Engagement: {scores["互动数"]}
186
  </span>
187
  </div>
188
  </div>
 
200
 
201
  return interface
202
 
203
+
204
+ @staticmethod
205
+ def _get_sentiment_label(sentiment_score: float) -> str:
206
+ if sentiment_score > 0.3:
207
+ return "Positive"
208
+ elif sentiment_score < -0.3:
209
+ return "Negative"
210
+ return "Neutral"
211
+
212
+ def _format_recommendations(self, recommendations: pd.DataFrame) -> Dict:
213
+ formatted_results = []
214
+ for _, row in recommendations.iterrows():
215
+ score_details = {
216
+ "总分": f"{row['Final_Score']:.2f}",
217
+ "可信度": "Reliable" if row['Credibility'] > 0 else "Uncertain",
218
+ "情感倾向": self._get_sentiment_label(row['Sentiment']),
219
+ "热度": f"{row['Popularity']:.2f}",
220
+ "互动数": f"Likes {row['Likes']} · Retweets {row['Retweets']}"
221
+ }
222
+
223
+ formatted_results.append({
224
+ "text": row['Text'],
225
+ "scores": score_details
226
+ })
227
+
228
+ return {
229
+ "recommendations": formatted_results,
230
+ "score_explanation": self._get_score_explanation()
231
+ }
232
+
233
+ @staticmethod
234
+ def _get_score_explanation() -> Dict[str, str]:
235
+ return {
236
+ "Credibility": "Content reliability assessment",
237
+ "Sentiment": "Text emotional analysis result",
238
+ "Popularity": "Score based on likes and retweets"
239
+ }
240
+
241
  def main():
242
  try:
243
  recommendation_system = RecommendationSystem(