diginoron commited on
Commit
46a0755
·
verified ·
1 Parent(s): c669c82

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -16
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import os
2
  import pandas as pd
3
  import gradio as gr
@@ -6,6 +7,7 @@ from huggingface_hub import InferenceClient
6
  from deep_translator import GoogleTranslator
7
  import spaces # برای مدیریت GPU کرایه‌ای
8
 
 
9
  HS_CSV_URL = (
10
  "https://raw.githubusercontent.com/"
11
  "datasets/harmonized-system/master/data/harmonized-system.csv"
@@ -17,6 +19,7 @@ def get_product_name(hs_code: str) -> str:
17
  row = hs_df[hs_df["hscode"] == code4]
18
  return row.iloc[0]["description"] if not row.empty else "–"
19
 
 
20
  def get_importers(hs_code: str, year: str, month: str):
21
  product_name = get_product_name(hs_code)
22
  period = f"{year}{int(month):02d}"
@@ -30,24 +33,19 @@ def get_importers(hs_code: str, year: str, month: str):
30
  if df is None or df.empty:
31
  return product_name, pd.DataFrame()
32
 
33
- std_map = {
34
- 'کد کشور': 'ptCode',
35
- 'نام کشور': 'ptTitle',
36
- 'ارزش CIF': 'TradeValue'
37
- }
38
- code_col = std_map['کد کشور'] if 'ptCode' in df.columns else next((c for c in df.columns if 'code' in c.lower()), None)
39
- title_col= std_map['نام کشور'] if 'ptTitle' in df.columns else next((c for c in df.columns if 'title' in c.lower()), None)
40
- value_col= std_map['ارزش CIF'] if 'TradeValue' in df.columns else next((c for c in df.columns if 'value' in c.lower()), None)
41
-
42
  if not (code_col and title_col and value_col):
43
  return product_name, df
44
 
45
  df_sorted = df.sort_values(value_col, ascending=False).head(10)
46
-
47
  out = df_sorted[[code_col, title_col, value_col]]
48
  out.columns = ['کد کشور', 'نام کشور', 'ارزش CIF']
49
  return product_name, out
50
 
 
51
  hf_token = os.getenv("HF_API_TOKEN")
52
  client = InferenceClient(token=hf_token)
53
  translator = GoogleTranslator(source='en', target='fa')
@@ -76,8 +74,19 @@ def provide_advice(table_data: pd.DataFrame, hs_code: str, year: str, month: str
76
  except Exception as e:
77
  return f"خطا در تولید مشاوره: {e}"
78
 
79
- with gr.Blocks() as demo:
80
- gr.Markdown("## تحلیل واردات بر اساس کد HS و ارائه مشاوره تخصصی")
 
 
 
 
 
 
 
 
 
 
 
81
 
82
  with gr.Row():
83
  inp_hs = gr.Textbox(label="کد HS", placeholder="مثلاً 1006")
@@ -85,9 +94,10 @@ with gr.Blocks() as demo:
85
  inp_month = gr.Textbox(label="ماه", placeholder="مثلاً 1 تا 12")
86
 
87
  btn_show = gr.Button("نمایش داده‌های واردات")
88
- out_name = gr.Markdown(label="**نام محصول**")
89
  out_table = gr.Dataframe(
90
- datatype="pandas",
 
91
  interactive=True
92
  )
93
 
@@ -98,7 +108,7 @@ with gr.Blocks() as demo:
98
  )
99
 
100
  btn_advice = gr.Button("ارائه مشاوره تخصصی")
101
- out_advice = gr.Textbox(label="مشاوره تخصصی", lines=8)
102
 
103
  btn_advice.click(
104
  fn=provide_advice,
@@ -107,4 +117,9 @@ with gr.Blocks() as demo:
107
  )
108
 
109
  if __name__ == "__main__":
110
- demo.launch()
 
 
 
 
 
 
1
+ # app.py
2
  import os
3
  import pandas as pd
4
  import gradio as gr
 
7
  from deep_translator import GoogleTranslator
8
  import spaces # برای مدیریت GPU کرایه‌ای
9
 
10
+ # --- بارگذاری HS DATA از CSV گیت‌هاب ---
11
  HS_CSV_URL = (
12
  "https://raw.githubusercontent.com/"
13
  "datasets/harmonized-system/master/data/harmonized-system.csv"
 
19
  row = hs_df[hs_df["hscode"] == code4]
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}"
 
33
  if df is None or df.empty:
34
  return product_name, pd.DataFrame()
35
 
36
+ # شناسایی ستون‌های مورد نیاز
37
+ code_col = next((c for c in df.columns if 'code' in c.lower()), None)
38
+ title_col = next((c for c in df.columns if 'title' in c.lower()), None)
39
+ value_col = next((c for c in df.columns if 'value' in c.lower()), None)
 
 
 
 
 
40
  if not (code_col and title_col and value_col):
41
  return product_name, df
42
 
43
  df_sorted = df.sort_values(value_col, ascending=False).head(10)
 
44
  out = df_sorted[[code_col, title_col, value_col]]
45
  out.columns = ['کد کشور', 'نام کشور', 'ارزش CIF']
46
  return product_name, out
47
 
48
+ # --- تابع تولید مشاوره تخصصی با GPU کرایه‌ای ---
49
  hf_token = os.getenv("HF_API_TOKEN")
50
  client = InferenceClient(token=hf_token)
51
  translator = GoogleTranslator(source='en', target='fa')
 
74
  except Exception as e:
75
  return f"خطا در تولید مشاوره: {e}"
76
 
77
+ # --- رابط کاربری Gradio با تنظیمات UI ---
78
+ with gr.Blocks(css="""
79
+ /* رنگ پس‌زمینه سفید و متن سیاه */
80
+ body, .gradio-container { background-color: white !important; color: black !important; }
81
+ /* پنهان کردن فوتر و لینک‌های Gradio */
82
+ footer, .gradio-info { display: none !important; }
83
+ """) as demo:
84
+ # عنوان سفارشی راست‌چین
85
+ gr.Markdown(
86
+ "<div dir='rtl' style='text-align: right; font-family: IRANSans;'>"
87
+ "<h2>هوش مصنوعی مشاوره صادراتی با HS Code محصول &ndash; ساخته شده توسط Diginoron</h2>"
88
+ "</div>"
89
+ )
90
 
91
  with gr.Row():
92
  inp_hs = gr.Textbox(label="کد HS", placeholder="مثلاً 1006")
 
94
  inp_month = gr.Textbox(label="ماه", placeholder="مثلاً 1 تا 12")
95
 
96
  btn_show = gr.Button("نمایش داده‌های واردات")
97
+ out_name = gr.Markdown(label="<div dir='rtl' style='text-align: right; font-family: IRANSans;'>**نام محصول**</div>")
98
  out_table = gr.Dataframe(
99
+ headers=['کد کشور', 'نام کشور', 'ارزش CIF'],
100
+ datatype=["number", "text", "number"],
101
  interactive=True
102
  )
103
 
 
108
  )
109
 
110
  btn_advice = gr.Button("ارائه مشاوره تخصصی")
111
+ out_advice = gr.Textbox(label="<div dir='rtl' style='text-align: right; font-family: IRANSans;'>مشاوره تخصصی</div>", lines=8)
112
 
113
  btn_advice.click(
114
  fn=provide_advice,
 
117
  )
118
 
119
  if __name__ == "__main__":
120
+ demo.launch(
121
+ share=True,
122
+ show_api=False,
123
+ show_tips=False,
124
+ show_share=False
125
+ )