Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -19,36 +19,50 @@ def get_product_name(hs_code: str) -> str:
|
|
19 |
row = hs_df[hs_df["hscode"] == code4]
|
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 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
# --- تابع تولید مشاوره تخصصی با GPU کرایهای ---
|
54 |
hf_token = os.getenv("HF_API_TOKEN")
|
@@ -79,42 +93,85 @@ def provide_advice(table_data: pd.DataFrame, hs_code: str, year: str, month: str
|
|
79 |
except Exception as e:
|
80 |
return f"خطا در تولید مشاوره: {e}"
|
81 |
|
82 |
-
# --- CSS برای مخفی کردن هدر و فوتر ---
|
83 |
custom_css = """
|
84 |
.gradio-container .header { display: none !important; }
|
85 |
.gradio-container .footer { display: none !important; }
|
86 |
.footer { display: none !important; }
|
87 |
.header { display: none !important; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
"""
|
89 |
|
90 |
# --- رابط کاربری سفارشی با Gradio Blocks ---
|
91 |
def create_custom_interface():
|
92 |
-
with gr.Blocks(css=custom_css) as demo:
|
93 |
# عنوان برنامه
|
94 |
gr.Markdown("## تحلیل واردات بر اساس کد HS و ارائه مشاوره تخصصی")
|
95 |
|
96 |
# بخش ورودیها
|
97 |
with gr.Row():
|
98 |
-
|
99 |
-
|
100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
|
102 |
-
# دکمه نمایش دادههای واردات
|
103 |
-
btn_show = gr.Button("نمایش دادههای واردات")
|
104 |
-
|
105 |
# خروجیها
|
106 |
-
|
107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
|
109 |
-
|
110 |
-
|
111 |
-
out_advice = gr.Textbox(label="مشاوره تخصصی", lines=8)
|
112 |
|
113 |
# اتصال دکمهها به توابع
|
114 |
btn_show.click(
|
115 |
fn=get_imports,
|
116 |
inputs=[inp_hs, inp_year, inp_month],
|
117 |
-
outputs=[out_name, out_table]
|
118 |
)
|
119 |
|
120 |
btn_advice.click(
|
@@ -128,4 +185,4 @@ def create_custom_interface():
|
|
128 |
# --- راهاندازی رابط کاربری ---
|
129 |
if __name__ == "__main__":
|
130 |
demo = create_custom_interface()
|
131 |
-
demo.launch(show_api=False)
|
|
|
19 |
row = hs_df[hs_df["hscode"] == code4]
|
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 |
+
try:
|
25 |
+
hs_code = str(hs_code).strip()
|
26 |
+
year = str(year).strip()
|
27 |
+
month = str(month).strip()
|
28 |
+
|
29 |
+
if not hs_code.isdigit() or len(hs_code) < 2:
|
30 |
+
return "–", pd.DataFrame(), "کد HS باید یک عدد معتبر باشد (حداقل 2 رقم)!"
|
31 |
+
if not year.isdigit() or int(year) < 2000 or int(year) > 2025:
|
32 |
+
return "–", pd.DataFrame(), "سال باید بین 2000 و 2025 باشد!"
|
33 |
+
if not month.isdigit() or int(month) < 1 or int(month) > 12:
|
34 |
+
return "–", pd.DataFrame(), "ماه باید بین 1 و 12 باشد!"
|
35 |
+
|
36 |
+
product_name = get_product_name(hs_code)
|
37 |
+
period = f"{year}{int(month):02d}"
|
38 |
+
df = comtradeapicall.previewFinalData(
|
39 |
+
typeCode='C', freqCode='M', clCode='HS', period=period,
|
40 |
+
reporterCode=None, cmdCode=hs_code, flowCode='M',
|
41 |
+
partnerCode=None, partner2Code=None,
|
42 |
+
customsCode=None, motCode=None,
|
43 |
+
maxRecords=500, includeDesc=True
|
44 |
+
)
|
45 |
+
if df is None or df.empty:
|
46 |
+
return product_name, pd.DataFrame(), "دادهای برای این کد HS و دوره زمانی یافت نشد."
|
47 |
+
|
48 |
+
std_map = {
|
49 |
+
'کد کشور': 'ptCode',
|
50 |
+
'نام کشور': 'ptTitle',
|
51 |
+
'ارزش CIF': 'TradeValue'
|
52 |
+
}
|
53 |
+
code_col = std_map['کد کشور'] if 'ptCode' in df.columns else next((c for c in df.columns if 'code' in c.lower()), None)
|
54 |
+
title_col = std_map['نام کشور'] if 'ptTitle' in df.columns else next((c for c in df.columns if 'title' in c.lower()), None)
|
55 |
+
value_col = std_map['ارزش CIF'] if 'TradeValue' in df.columns else next((c for c in df.columns if 'value' in c.lower()), None)
|
56 |
+
|
57 |
+
if not (code_col and title_col and value_col):
|
58 |
+
return product_name, df, "ستونهای مورد نیاز در دادهها یافت نشد."
|
59 |
+
|
60 |
+
df_sorted = df.sort_values(value_col, ascending=False).head(10)
|
61 |
+
out = df_sorted[[code_col, title_col, value_col]]
|
62 |
+
out.columns = ['کد کشور', 'نام کشور', 'ارزش CIF']
|
63 |
+
return product_name, out, ""
|
64 |
+
except Exception as e:
|
65 |
+
return "–", pd.DataFrame(), f"خطا: {str(e)}"
|
66 |
|
67 |
# --- تابع تولید مشاوره تخصصی با GPU کرایهای ---
|
68 |
hf_token = os.getenv("HF_API_TOKEN")
|
|
|
93 |
except Exception as e:
|
94 |
return f"خطا در تولید مشاوره: {e}"
|
95 |
|
96 |
+
# --- CSS برای مخفی کردن هدر و فوتر و استایل بصری ---
|
97 |
custom_css = """
|
98 |
.gradio-container .header { display: none !important; }
|
99 |
.gradio-container .footer { display: none !important; }
|
100 |
.footer { display: none !important; }
|
101 |
.header { display: none !important; }
|
102 |
+
|
103 |
+
body {
|
104 |
+
background-color: #1a1a1a;
|
105 |
+
color: #e0e0e0;
|
106 |
+
}
|
107 |
+
#input-hs, #input-year, #input-month {
|
108 |
+
background-color: #2c2c2c;
|
109 |
+
color: #e0e0e0;
|
110 |
+
border: 1px solid #444;
|
111 |
+
border-radius: 5px;
|
112 |
+
padding: 5px;
|
113 |
+
}
|
114 |
+
button {
|
115 |
+
margin: 5px;
|
116 |
+
padding: 10px 20px;
|
117 |
+
border-radius: 5px;
|
118 |
+
}
|
119 |
+
.gr-Markdown {
|
120 |
+
padding: 10px;
|
121 |
+
background-color: #2c2c2c;
|
122 |
+
border-radius: 5px;
|
123 |
+
}
|
124 |
+
.gr-Dataframe, .gr-Textbox {
|
125 |
+
background-color: #2c2c2c;
|
126 |
+
color: #e0e0e0;
|
127 |
+
border: 1px solid #444;
|
128 |
+
border-radius: 5px;
|
129 |
+
padding: 10px;
|
130 |
+
}
|
131 |
"""
|
132 |
|
133 |
# --- رابط کاربری سفارشی با Gradio Blocks ---
|
134 |
def create_custom_interface():
|
135 |
+
with gr.Blocks(css=custom_css) as demo:
|
136 |
# عنوان برنامه
|
137 |
gr.Markdown("## تحلیل واردات بر اساس کد HS و ارائه مشاوره تخصصی")
|
138 |
|
139 |
# بخش ورودیها
|
140 |
with gr.Row():
|
141 |
+
with gr.Column(scale=1):
|
142 |
+
inp_hs = gr.Textbox(label="کد HS", placeholder="مثلاً 1006", elem_id="input-hs")
|
143 |
+
with gr.Column(scale=1):
|
144 |
+
inp_year = gr.Dropdown(label="سال", choices=[str(i) for i in range(2000, 2026)], value="2023")
|
145 |
+
with gr.Column(scale=1):
|
146 |
+
inp_month = gr.Dropdown(label="ماه", choices=[str(i).zfill(2) for i in range(1, 13)], value="01")
|
147 |
+
|
148 |
+
# دکمهها
|
149 |
+
with gr.Row():
|
150 |
+
btn_show = gr.Button("نمایش دادههای واردات", variant="primary")
|
151 |
+
btn_advice = gr.Button("ارائه مشاوره تخصصی", variant="secondary")
|
152 |
|
|
|
|
|
|
|
153 |
# خروجیها
|
154 |
+
with gr.Row():
|
155 |
+
out_name = gr.Markdown(label="**نام محصول**")
|
156 |
+
out_table = gr.Dataframe(
|
157 |
+
datatype="pandas",
|
158 |
+
interactive=True,
|
159 |
+
headers=["کد کشور", "نام کشور", "ارزش CIF"],
|
160 |
+
wrap=True,
|
161 |
+
overflow="wrap",
|
162 |
+
file_type="csv",
|
163 |
+
file_name="import_data.csv"
|
164 |
+
)
|
165 |
+
out_message = gr.Markdown(label="پیام", visible=True)
|
166 |
|
167 |
+
with gr.Row():
|
168 |
+
out_advice = gr.Textbox(label="مشاوره تخصصی", lines=8)
|
|
|
169 |
|
170 |
# اتصال دکمهها به توابع
|
171 |
btn_show.click(
|
172 |
fn=get_imports,
|
173 |
inputs=[inp_hs, inp_year, inp_month],
|
174 |
+
outputs=[out_name, out_table, out_message]
|
175 |
)
|
176 |
|
177 |
btn_advice.click(
|
|
|
185 |
# --- راهاندازی رابط کاربری ---
|
186 |
if __name__ == "__main__":
|
187 |
demo = create_custom_interface()
|
188 |
+
demo.launch(show_api=False, theme="huggingface/dark")
|