Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
@@ -1,193 +0,0 @@
|
|
1 |
-
# app.py
|
2 |
-
import os
|
3 |
-
import pandas as pd
|
4 |
-
import gradio as gr
|
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 = (
|
12 |
-
"https://raw.githubusercontent.com/"
|
13 |
-
"datasets/harmonized-system/master/data/harmonized-system.csv"
|
14 |
-
)
|
15 |
-
hs_df = pd.read_csv(HS_CSV_URL, dtype=str)
|
16 |
-
|
17 |
-
def get_product_name(hs_code: str) -> str:
|
18 |
-
code4 = str(hs_code).zfill(4)
|
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 |
-
return product_name, df_sorted, "" # بازگشت تمام ستونها
|
62 |
-
except Exception as e:
|
63 |
-
return "–", pd.DataFrame(), f"خطا: {str(e)}"
|
64 |
-
|
65 |
-
# --- تابع تولید مشاوره تخصصی با GPU کرایهای ---
|
66 |
-
hf_token = os.getenv("HF_API_TOKEN")
|
67 |
-
client = InferenceClient(token=hf_token)
|
68 |
-
translator = GoogleTranslator(source='en', target='fa')
|
69 |
-
|
70 |
-
@spaces.GPU
|
71 |
-
def provide_advice(table_data: pd.DataFrame, hs_code: str, year: str, month: str):
|
72 |
-
if table_data is None or table_data.empty:
|
73 |
-
return "ابتدا نمایش دادههای واردات را انجام دهید."
|
74 |
-
|
75 |
-
df_limited = table_data.head(10)
|
76 |
-
table_str = df_limited.to_string(index=False)
|
77 |
-
period = f"{year}/{int(month):02d}"
|
78 |
-
prompt = (
|
79 |
-
f"The following table shows the top {len(df_limited)} countries by CIF value importing HS code {hs_code} during {period}:\n"
|
80 |
-
f"{table_str}\n\n"
|
81 |
-
"Please provide a detailed and comprehensive analysis of market trends, risks, "
|
82 |
-
"and opportunities for a new exporter entering this market."
|
83 |
-
)
|
84 |
-
try:
|
85 |
-
outputs = client.text_generation(
|
86 |
-
prompt=prompt,
|
87 |
-
model="mistralai/Mixtral-8x7B-Instruct-v0.1",
|
88 |
-
max_new_tokens=1024
|
89 |
-
)
|
90 |
-
return translator.translate(outputs)
|
91 |
-
except Exception as e:
|
92 |
-
return f"خطا در تولید مشاوره: {e}"
|
93 |
-
|
94 |
-
# --- CSS برای مخفی کردن هدر و فوتر و استایل بصری ---
|
95 |
-
custom_css = """
|
96 |
-
.gradio-container .header { display: none !important; }
|
97 |
-
.gradio-container .footer { display: none !important; }
|
98 |
-
.footer { display: none !important; }
|
99 |
-
.header { display: none !important; }
|
100 |
-
|
101 |
-
body {
|
102 |
-
background-color: #1a1a1a;
|
103 |
-
color: #e0e0e0;
|
104 |
-
}
|
105 |
-
#input-hs {
|
106 |
-
background-color: #2c2c2c;
|
107 |
-
color: #e0e0e0;
|
108 |
-
border: 1px solid #444;
|
109 |
-
border-radius: 5px;
|
110 |
-
padding: 5px;
|
111 |
-
}
|
112 |
-
button {
|
113 |
-
margin: 5px;
|
114 |
-
padding: 10px 20px;
|
115 |
-
border-radius: 5px;
|
116 |
-
}
|
117 |
-
.gr-Markdown {
|
118 |
-
padding: 10px;
|
119 |
-
background-color: #2c2c2c;
|
120 |
-
border-radius: 5px;
|
121 |
-
}
|
122 |
-
.gr-Dataframe, .gr-Textbox {
|
123 |
-
background-color: #2c2c2c;
|
124 |
-
color: #e0e0e0;
|
125 |
-
border: 1px solid #444;
|
126 |
-
border-radius: 5px;
|
127 |
-
padding: 10px;
|
128 |
-
}
|
129 |
-
.gr-Dataframe table {
|
130 |
-
width: 100% !important;
|
131 |
-
table-layout: auto !important;
|
132 |
-
}
|
133 |
-
.gr-Dataframe th, .gr-Dataframe td {
|
134 |
-
min-width: 100px !important; /* حداقل عرض برای ستونها */
|
135 |
-
max-width: 250px !important; /* حداکثر عرض برای جلوگیری از شلوغی */
|
136 |
-
white-space: normal !important;
|
137 |
-
word-wrap: break-word !important;
|
138 |
-
text-align: center !important;
|
139 |
-
padding: 5px !important; /* فاصله داخلی برای خوانایی بهتر */
|
140 |
-
}
|
141 |
-
"""
|
142 |
-
|
143 |
-
# --- رابط کاربری سفارشی با Gradio Blocks ---
|
144 |
-
def create_custom_interface():
|
145 |
-
with gr.Blocks(css=custom_css) as demo:
|
146 |
-
# عنوان برنامه
|
147 |
-
gr.Markdown("## تحلیل واردات بر اساس کد HS و ارائه مشاوره تخصصی")
|
148 |
-
|
149 |
-
# بخش ورودیها
|
150 |
-
with gr.Row():
|
151 |
-
with gr.Column(scale=1):
|
152 |
-
inp_hs = gr.Textbox(label="کد HS", placeholder="مثلاً 1006", elem_id="input-hs")
|
153 |
-
with gr.Column(scale=1):
|
154 |
-
inp_year = gr.Dropdown(label="سال", choices=[str(i) for i in range(2000, 2026)], value="2023")
|
155 |
-
with gr.Column(scale=1):
|
156 |
-
inp_month = gr.Dropdown(label="ماه", choices=[str(i).zfill(2) for i in range(1, 13)], value="01")
|
157 |
-
|
158 |
-
# دکمهها
|
159 |
-
with gr.Row():
|
160 |
-
btn_show = gr.Button("نمایش دادههای واردات", variant="primary")
|
161 |
-
btn_advice = gr.Button("ارائه مشاوره تخصصی", variant="secondary")
|
162 |
-
|
163 |
-
# خروجیها
|
164 |
-
with gr.Row():
|
165 |
-
out_name = gr.Markdown(label="**نام محصول**")
|
166 |
-
out_table = gr.Dataframe(
|
167 |
-
datatype="pandas",
|
168 |
-
interactive=True
|
169 |
-
)
|
170 |
-
out_message = gr.Markdown(label="پیام", visible=True)
|
171 |
-
|
172 |
-
with gr.Row():
|
173 |
-
out_advice = gr.Textbox(label="مشاوره تخصصی", lines=8)
|
174 |
-
|
175 |
-
# اتصال دکمهها به توابع
|
176 |
-
btn_show.click(
|
177 |
-
fn=get_imports,
|
178 |
-
inputs=[inp_hs, inp_year, inp_month],
|
179 |
-
outputs=[out_name, out_table, out_message]
|
180 |
-
)
|
181 |
-
|
182 |
-
btn_advice.click(
|
183 |
-
fn=provide_advice,
|
184 |
-
inputs=[out_table, inp_hs, inp_year, inp_month],
|
185 |
-
outputs=out_advice
|
186 |
-
)
|
187 |
-
|
188 |
-
return demo
|
189 |
-
|
190 |
-
# --- راهاندازی رابط کاربری ---
|
191 |
-
if __name__ == "__main__":
|
192 |
-
demo = create_custom_interface()
|
193 |
-
demo.launch(show_api=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|