diginoron commited on
Commit
60910cb
·
verified ·
1 Parent(s): cbb8f5b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -39
app.py CHANGED
@@ -5,7 +5,7 @@ import gradio as gr
5
  import comtradeapicall
6
  from huggingface_hub import InferenceClient
7
  from deep_translator import GoogleTranslator
8
- import spaces # برای مدیریت GPU کرایه‌ای
9
 
10
  # --- بارگذاری HS DATA از CSV گیت‌هاب ---
11
  HS_CSV_URL = (
@@ -20,7 +20,7 @@ def get_product_name(hs_code: str) -> str:
20
  return row.iloc[0]["description"] if not row.empty else "–"
21
 
22
  # --- تابع دریافت واردات و پردازش ستون‌ها ---
23
- def get_importers(hs_code: str, year: str, month: str):
24
  product_name = get_product_name(hs_code)
25
  period = f"{year}{int(month):02d}"
26
  df = comtradeapicall.previewFinalData(
@@ -33,24 +33,19 @@ def get_importers(hs_code: str, year: str, month: str):
33
  if df is None or df.empty:
34
  return product_name, pd.DataFrame()
35
 
36
- # شناسایی ستون‌های مورد نیاز (کد کشور، نام کشور، ارزش)
37
- # ابتدا سعی در استفاده از ستون‌های استاندارد
38
  std_map = {
39
  'کد کشور': 'ptCode',
40
  'نام کشور': 'ptTitle',
41
  'ارزش CIF': 'TradeValue'
42
  }
43
  code_col = std_map['کد کشور'] if 'ptCode' in df.columns else next((c for c in df.columns if 'code' in c.lower()), None)
44
- title_col= std_map['نام کشور'] if 'ptTitle' in df.columns else next((c for c in df.columns if 'title' in c.lower()), None)
45
- value_col= std_map['ارزش CIF'] if 'TradeValue' in df.columns else next((c for c in df.columns if 'value' in c.lower()), None)
46
 
47
  if not (code_col and title_col and value_col):
48
- # اگر نتوانست ستون‌ها را شناسایی کند، برگرداندن DataFrame خام
49
  return product_name, df
50
 
51
- # محدودسازی به 10 کشور برتر بر اساس ستون value_col
52
  df_sorted = df.sort_values(value_col, ascending=False).head(10)
53
-
54
  out = df_sorted[[code_col, title_col, value_col]]
55
  out.columns = ['کد کشور', 'نام کشور', 'ارزش CIF']
56
  return product_name, out
@@ -65,7 +60,6 @@ def provide_advice(table_data: pd.DataFrame, hs_code: str, year: str, month: str
65
  if table_data is None or table_data.empty:
66
  return "ابتدا نمایش داده‌های واردات را انجام دهید."
67
 
68
- # محدودسازی تعداد ردیف‌های ورودی به 10 (در صورت بیشتر)
69
  df_limited = table_data.head(10)
70
  table_str = df_limited.to_string(index=False)
71
  period = f"{year}/{int(month):02d}"
@@ -85,36 +79,45 @@ def provide_advice(table_data: pd.DataFrame, hs_code: str, year: str, month: str
85
  except Exception as e:
86
  return f"خطا در تولید مشاوره: {e}"
87
 
88
- # --- رابط کاربری Gradio ---
89
- with gr.Blocks() as demo:
90
- gr.Markdown("## تحلیل واردات بر اساس کد HS و ارائه مشاوره تخصصی")
91
-
92
- with gr.Row():
93
- inp_hs = gr.Textbox(label="کد HS", placeholder="مثلاً 1006")
94
- inp_year = gr.Textbox(label="سال", placeholder="مثلاً 2023")
95
- inp_month = gr.Textbox(label="ماه", placeholder="مثلاً 1 تا 12")
96
-
97
- btn_show = gr.Button("نمایش داده‌های واردات")
98
- out_name = gr.Markdown(label="**نام محصول**")
99
- out_table = gr.Dataframe(
100
- datatype="pandas",
101
- interactive=True
102
- )
103
-
104
- btn_show.click(
105
- fn=get_importers,
106
- inputs=[inp_hs, inp_year, inp_month],
107
- outputs=[out_name, out_table]
108
- )
 
 
 
 
 
 
 
 
109
 
110
- btn_advice = gr.Button("ارائه مشاوره تخصصی")
111
- out_advice = gr.Textbox(label="مشاوره تخصصی", lines=8)
 
 
 
112
 
113
- btn_advice.click(
114
- fn=provide_advice,
115
- inputs=[out_table, inp_hs, inp_year, inp_month],
116
- outputs=out_advice
117
- )
118
 
 
119
  if __name__ == "__main__":
120
- demo.launch()
 
 
5
  import comtradeapicall
6
  from huggingface_hub import InferenceClient
7
  from deep_translator import GoogleTranslator
8
+ import spaces
9
 
10
  # --- بارگذاری HS DATA از CSV گیت‌هاب ---
11
  HS_CSV_URL = (
 
20
  return row.iloc[0]["description"] if not row.empty else "–"
21
 
22
  # --- تابع دریافت واردات و پردازش ستون‌ها ---
23
+ def get_imports(hs_code: str, year: str, month: str):
24
  product_name = get_product_name(hs_code)
25
  period = f"{year}{int(month):02d}"
26
  df = comtradeapicall.previewFinalData(
 
33
  if df is None or df.empty:
34
  return product_name, pd.DataFrame()
35
 
 
 
36
  std_map = {
37
  'کد کشور': 'ptCode',
38
  'نام کشور': 'ptTitle',
39
  'ارزش CIF': 'TradeValue'
40
  }
41
  code_col = std_map['کد کشور'] if 'ptCode' in df.columns else next((c for c in df.columns if 'code' in c.lower()), None)
42
+ title_col = std_map['نام کشور'] if 'ptTitle' in df.columns else next((c for c in df.columns if 'title' in c.lower()), None)
43
+ value_col = std_map['ارزش CIF'] if 'TradeValue' in df.columns else next((c for c in df.columns if 'value' in c.lower()), None)
44
 
45
  if not (code_col and title_col and value_col):
 
46
  return product_name, df
47
 
 
48
  df_sorted = df.sort_values(value_col, ascending=False).head(10)
 
49
  out = df_sorted[[code_col, title_col, value_col]]
50
  out.columns = ['کد کشور', 'نام کشور', 'ارزش CIF']
51
  return product_name, out
 
60
  if table_data is None or table_data.empty:
61
  return "ابتدا نمایش داده‌های واردات را انجام دهید."
62
 
 
63
  df_limited = table_data.head(10)
64
  table_str = df_limited.to_string(index=False)
65
  period = f"{year}/{int(month):02d}"
 
79
  except Exception as e:
80
  return f"خطا در تولید مشاوره: {e}"
81
 
82
+ # --- رابط کاربری سفارشی با Gradio Blocks ---
83
+ def create_custom_interface():
84
+ with gr.Blocks() as demo:
85
+ # عنوان برنامه
86
+ gr.Markdown("## تحلیل واردات بر اساس کد HS و ارائه مشاوره تخصصی")
87
+
88
+ # بخش ورودی‌ها
89
+ with gr.Row():
90
+ inp_hs = gr.Textbox(label="کد HS", placeholder="مثلاً 1006")
91
+ inp_year = gr.Textbox(label="سال", placeholder="مثلاً 2023")
92
+ inp_month = gr.Textbox(label="ماه", placeholder="مثلاً 1 تا 12")
93
+
94
+ # دکمه نمایش داده‌های واردات
95
+ btn_show = gr.Button("نمایش داده‌های واردات")
96
+
97
+ # خروجی‌ها
98
+ out_name = gr.Markdown(label="**نام محصول**")
99
+ out_table = gr.Dataframe(datatype="pandas", interactive=True)
100
+
101
+ # دکمه ارائه مشاوره
102
+ btn_advice = gr.Button("ارائه مشاوره تخصصی")
103
+ out_advice = gr.Textbox(label="مشاوره تخصصی", lines=8)
104
+
105
+ # اتصال دکمه‌ها به توابع
106
+ btn_show.click(
107
+ fn=get_importers,
108
+ inputs=[inp_hs, inp_year, inp_month],
109
+ outputs=[out_name, out_table]
110
+ )
111
 
112
+ btn_advice.click(
113
+ fn=provide_advice,
114
+ inputs=[out_table, inp_hs, inp_year, inp_month],
115
+ outputs=out_advice
116
+ )
117
 
118
+ return demo
 
 
 
 
119
 
120
+ # --- راه‌اندازی رابط کاربری ---
121
  if __name__ == "__main__":
122
+ demo = create_custom_interface()
123
+ demo.launch(show_api=False)