54justin commited on
Commit
f3d41f0
·
verified ·
1 Parent(s): e538e84

Upload 13 files

Browse files
Files changed (6) hide show
  1. app.py +19 -2
  2. gradio_app.py +13 -3
  3. rental_analyzer.py +9 -4
  4. requirements.txt +11 -13
  5. spaces_config.py +48 -0
  6. test_imports.py +54 -0
app.py CHANGED
@@ -2,9 +2,26 @@
2
  # 591���Τ��R�� - Hugging Face Spaces����
3
  # �ϥ�Gradio�@���D�n����
4
 
 
5
  from gradio_app import create_interface
6
 
7
  # �Ұ�Gradio����
8
  if __name__ == "__main__":
9
- demo = create_interface()
10
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  # 591���Τ��R�� - Hugging Face Spaces����
3
  # �ϥ�Gradio�@���D�n����
4
 
5
+ import spaces_config # ���J Spaces �t�m
6
  from gradio_app import create_interface
7
 
8
  # �Ұ�Gradio����
9
  if __name__ == "__main__":
10
+ try:
11
+ demo = create_interface()
12
+ demo.launch()
13
+ except Exception as e:
14
+ print(f"? �Ұ����Υ���: {e}")
15
+ # ���ըϥγ�²�檺�t�m
16
+ import gradio as gr
17
+
18
+ def simple_interface():
19
+ return "? 591���Τ��R�����b��l�ơA�еy��A��..."
20
+
21
+ simple_demo = gr.Interface(
22
+ fn=simple_interface,
23
+ inputs=[],
24
+ outputs="text",
25
+ title="591�����R��"
26
+ )
27
+ simple_demo.launch()
gradio_app.py CHANGED
@@ -11,20 +11,30 @@ from datetime import datetime
11
  from rental_analyzer import RentalAnalyzer
12
  from data_generator import generate_mock_rental_data, get_market_summary_stats
13
 
14
- # �]�w����r��
15
- plt.rcParams['font.sans-serif'] = ['DejaVu Sans', 'Arial Unicode MS', 'SimHei']
16
- plt.rcParams['axes.unicode_minus'] = False
 
 
 
17
 
18
  def analyze_rental_data(sample_size, use_hf_models):
19
  """���毲�Τ��R���D���"""
20
 
21
  try:
 
 
 
 
22
  # �B�J1: �ͦ��������
23
  progress_info = "? ���b�ͦ����R���..."
24
 
25
  data = generate_mock_rental_data(int(sample_size))
26
  df = pd.DataFrame(data)
27
 
 
 
 
28
  # �B�J2: ������R
29
  progress_info = "? ���b����έp���R..."
30
 
 
11
  from rental_analyzer import RentalAnalyzer
12
  from data_generator import generate_mock_rental_data, get_market_summary_stats
13
 
14
+ # �]�wr��]Hugging Face Spaces �ۮe�^
15
+ try:
16
+ plt.rcParams['font.sans-serif'] = ['DejaVu Sans', 'Arial']
17
+ plt.rcParams['axes.unicode_minus'] = False
18
+ except Exception:
19
+ pass # �����r��]�w���~
20
 
21
  def analyze_rental_data(sample_size, use_hf_models):
22
  """���毲�Τ��R���D���"""
23
 
24
  try:
25
+ # �T�O sample_size �����ļƭ�
26
+ if sample_size is None or sample_size <= 0:
27
+ sample_size = 50
28
+
29
  # �B�J1: �ͦ��������
30
  progress_info = "? ���b�ͦ����R���..."
31
 
32
  data = generate_mock_rental_data(int(sample_size))
33
  df = pd.DataFrame(data)
34
 
35
+ if df.empty:
36
+ raise ValueError("�ͦ�����Ƭ���")
37
+
38
  # �B�J2: ������R
39
  progress_info = "? ���b����έp���R..."
40
 
rental_analyzer.py CHANGED
@@ -25,15 +25,20 @@ class RentalAnalyzer:
25
  self.sentiment_analyzer = None
26
  if use_hf_models:
27
  try:
28
- # ���J���屡�P���R�ҫ�
29
  self.sentiment_analyzer = pipeline(
30
  "sentiment-analysis",
31
- model="ckiplab/bert-base-chinese",
32
- return_all_scores=True
33
  )
34
  except Exception as e:
35
  print(f"Warning: Could not load Hugging Face model: {e}")
36
- self.use_hf_models = False
 
 
 
 
 
37
 
38
  def clean_data(self) -> pd.DataFrame:
39
  """�M�~���"""
 
25
  self.sentiment_analyzer = None
26
  if use_hf_models:
27
  try:
28
+ # �ϥθ��p���^�屡�P���R�ҫ��A�קK���J���D
29
  self.sentiment_analyzer = pipeline(
30
  "sentiment-analysis",
31
+ model="cardiffnlp/twitter-roberta-base-sentiment-latest",
32
+ return_all_scores=False
33
  )
34
  except Exception as e:
35
  print(f"Warning: Could not load Hugging Face model: {e}")
36
+ # ���ըϥιw�]�ҫ�
37
+ try:
38
+ self.sentiment_analyzer = pipeline("sentiment-analysis")
39
+ except Exception as e2:
40
+ print(f"Warning: Could not load any sentiment model: {e2}")
41
+ self.use_hf_models = False
42
 
43
  def clean_data(self) -> pd.DataFrame:
44
  """�M�~���"""
requirements.txt CHANGED
@@ -1,13 +1,11 @@
1
- # �� Copilot �ͦ� - Hugging Face Spaces �ۮe����
2
- streamlit>=1.28.0
3
- gradio>=3.50.0
4
- pandas>=2.0.0
5
- numpy>=1.24.0
6
- matplotlib>=3.7.0
7
- seaborn>=0.12.0
8
- plotly>=5.15.0
9
- requests>=2.31.0
10
- beautifulsoup4>=4.12.0
11
- transformers>=4.30.0
12
- datasets>=2.14.0
13
- scikit-learn>=1.3.0
 
1
+ # �� Copilot �ͦ� - Hugging Face Spaces �̤p�ۮe����
2
+ pandas
3
+ numpy
4
+ matplotlib
5
+ seaborn
6
+ plotly
7
+ requests
8
+ beautifulsoup4
9
+ transformers
10
+ datasets
11
+ scikit-learn
 
 
spaces_config.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # �� Copilot �ͦ�
2
+ # Hugging Face Spaces �t�m�]�w
3
+
4
+ import os
5
+ import warnings
6
+
7
+ # �]�w�����ܼơA�u�� Hugging Face Spaces �B��
8
+ os.environ['TOKENIZERS_PARALLELISM'] = 'false'
9
+ os.environ['HF_HUB_DISABLE_PROGRESS_BARS'] = '1'
10
+
11
+ # �����Y��ĵ�i
12
+ warnings.filterwarnings('ignore', category=UserWarning)
13
+ warnings.filterwarnings('ignore', category=FutureWarning)
14
+
15
+ # Hugging Face Spaces �S�w�]�w
16
+ HF_SPACES_CONFIG = {
17
+ 'max_sample_size': 100, # ����˥��ƶq�קK�W��
18
+ 'enable_hf_models': True, # �w�]�ҥ� HF �ҫ�
19
+ 'timeout': 60, # 60���W��
20
+ 'model_cache_dir': '/tmp/hf_cache', # �ҫ��֨��ؿ�
21
+ }
22
+
23
+ def is_running_on_spaces():
24
+ """�ˬd�O�_�B��b Hugging Face Spaces �W"""
25
+ return os.getenv('SPACE_ID') is not None
26
+
27
+ def get_spaces_config():
28
+ """��� Spaces �t�m"""
29
+ return HF_SPACES_CONFIG
30
+
31
+ def setup_for_spaces():
32
+ """�� Spaces ���Ҷi��]�w"""
33
+ if is_running_on_spaces():
34
+ print("? Running on Hugging Face Spaces")
35
+
36
+ # �]�w�֨��ؿ�
37
+ cache_dir = HF_SPACES_CONFIG['model_cache_dir']
38
+ if not os.path.exists(cache_dir):
39
+ os.makedirs(cache_dir, exist_ok=True)
40
+
41
+ # �]�w transformers �֨�
42
+ os.environ['TRANSFORMERS_CACHE'] = cache_dir
43
+ os.environ['HF_HOME'] = cache_dir
44
+ else:
45
+ print("? Running locally")
46
+
47
+ # �۰ʰ���]�w
48
+ setup_for_spaces()
test_imports.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # �� Copilot �ͦ�
2
+ # ���թҦ��ҲլO�_�ॿ�`�ɤJ
3
+
4
+ try:
5
+ print("Testing imports...")
6
+
7
+ import pandas as pd
8
+ print("? pandas")
9
+
10
+ import numpy as np
11
+ print("? numpy")
12
+
13
+ import matplotlib.pyplot as plt
14
+ print("? matplotlib")
15
+
16
+ import seaborn as sns
17
+ print("? seaborn")
18
+
19
+ import plotly.express as px
20
+ print("? plotly")
21
+
22
+ from data_generator import generate_mock_rental_data
23
+ print("? data_generator")
24
+
25
+ from rental_analyzer import RentalAnalyzer
26
+ print("? rental_analyzer")
27
+
28
+ import gradio as gr
29
+ print("? gradio")
30
+
31
+ print("\n? All imports successful!")
32
+
33
+ # ���հ򥻥\��
34
+ print("\nTesting basic functionality...")
35
+
36
+ # �ͦ����ո��
37
+ data = generate_mock_rental_data(5)
38
+ print(f"? Generated {len(data)} sample records")
39
+
40
+ # �Ы� DataFrame
41
+ df = pd.DataFrame(data)
42
+ print(f"? Created DataFrame with {len(df)} rows")
43
+
44
+ # ���դ��R���]���ϥ� HF �ҫ��^
45
+ analyzer = RentalAnalyzer(df, use_hf_models=False)
46
+ results = analyzer.run_analysis()
47
+ print(f"? Analysis completed with {len(results)} result categories")
48
+
49
+ print("\n? All tests passed!")
50
+
51
+ except Exception as e:
52
+ print(f"? Error: {e}")
53
+ import traceback
54
+ traceback.print_exc()