Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,7 @@
|
|
1 |
# app.py
|
2 |
import os
|
3 |
-
import json
|
4 |
-
from pathlib import Path
|
5 |
-
|
6 |
-
import requests
|
7 |
-
import gradio as gr
|
8 |
import pandas as pd
|
|
|
9 |
import comtradeapicall
|
10 |
from huggingface_hub import InferenceClient
|
11 |
from deep_translator import GoogleTranslator
|
@@ -19,11 +15,14 @@ HS_CSV_URL = (
|
|
19 |
hs_df = pd.read_csv(HS_CSV_URL, dtype=str)
|
20 |
|
21 |
def get_product_name(hs_code: str) -> str:
|
|
|
|
|
|
|
|
|
22 |
code4 = str(hs_code).zfill(4)
|
23 |
row = hs_df[hs_df["hscode"] == code4]
|
24 |
return row.iloc[0]["description"] if not row.empty else "–"
|
25 |
|
26 |
-
|
27 |
# --- تابع دریافت واردات و پردازش ستونها ---
|
28 |
def get_importers(hs_code: str, year: str, month: str):
|
29 |
product_name = get_product_name(hs_code)
|
@@ -38,40 +37,44 @@ def get_importers(hs_code: str, year: str, month: str):
|
|
38 |
if df is None or df.empty:
|
39 |
return product_name, pd.DataFrame()
|
40 |
|
41 |
-
#
|
|
|
|
|
|
|
42 |
std_cols = ['ptCode', 'ptTitle', 'TradeValue']
|
43 |
if all(col in df.columns for col in std_cols):
|
44 |
out = df[std_cols]
|
45 |
out.columns = ['کد کشور', 'نام کشور', 'ارزش CIF']
|
46 |
return product_name, out
|
47 |
|
48 |
-
#
|
49 |
-
code_col = next((c for c in df.columns if 'code'
|
50 |
-
title_col= next((c for c in df.columns if 'title' in c.lower()), None)
|
51 |
-
value_col= next((c for c in df.columns if 'value' in c.lower()), None)
|
52 |
-
|
53 |
if code_col and title_col and value_col:
|
54 |
out = df[[code_col, title_col, value_col]]
|
55 |
out.columns = ['کد کشور', 'نام کشور', 'ارزش CIF']
|
56 |
return product_name, out
|
57 |
|
58 |
-
# اگر نشد،
|
59 |
return product_name, df
|
60 |
|
61 |
-
|
62 |
# --- تابع تولید مشاوره تخصصی با GPU کرایهای ---
|
63 |
-
hf_token
|
64 |
-
client
|
65 |
translator = GoogleTranslator(source='en', target='fa')
|
66 |
|
67 |
@spaces.GPU
|
68 |
def provide_advice(table_data: pd.DataFrame, hs_code: str, year: str, month: str):
|
69 |
if table_data is None or table_data.empty:
|
70 |
return "ابتدا نمایش دادههای واردات را انجام دهید."
|
71 |
-
|
|
|
|
|
|
|
72 |
period = f"{year}/{int(month):02d}"
|
73 |
prompt = (
|
74 |
-
f"The following table shows countries
|
75 |
f"{table_str}\n\n"
|
76 |
"Please provide a detailed and comprehensive analysis of market trends, risks, "
|
77 |
"and opportunities for a new exporter entering this market."
|
@@ -86,18 +89,17 @@ def provide_advice(table_data: pd.DataFrame, hs_code: str, year: str, month: str
|
|
86 |
except Exception as e:
|
87 |
return f"خطا در تولید مشاوره: {e}"
|
88 |
|
89 |
-
|
90 |
# --- رابط کاربری Gradio ---
|
91 |
with gr.Blocks() as demo:
|
92 |
gr.Markdown("## تحلیل واردات بر اساس کد HS و ارائه مشاوره تخصصی")
|
93 |
|
94 |
with gr.Row():
|
95 |
-
inp_hs
|
96 |
-
inp_year
|
97 |
-
inp_month = gr.Textbox(label="ماه",
|
98 |
|
99 |
-
btn_show
|
100 |
-
out_name
|
101 |
out_table = gr.Dataframe(
|
102 |
datatype="pandas",
|
103 |
interactive=True
|
|
|
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
|
|
|
15 |
hs_df = pd.read_csv(HS_CSV_URL, dtype=str)
|
16 |
|
17 |
def get_product_name(hs_code: str) -> str:
|
18 |
+
"""
|
19 |
+
از DataFrame محلی hs_df نام محصول متناظر با کد HS را میگیرد.
|
20 |
+
اگر یافت نشد، '–' برمیگرداند.
|
21 |
+
"""
|
22 |
code4 = str(hs_code).zfill(4)
|
23 |
row = hs_df[hs_df["hscode"] == code4]
|
24 |
return row.iloc[0]["description"] if not row.empty else "–"
|
25 |
|
|
|
26 |
# --- تابع دریافت واردات و پردازش ستونها ---
|
27 |
def get_importers(hs_code: str, year: str, month: str):
|
28 |
product_name = get_product_name(hs_code)
|
|
|
37 |
if df is None or df.empty:
|
38 |
return product_name, pd.DataFrame()
|
39 |
|
40 |
+
# محدود کردن تعداد سطر برای جلوگیری از خطای بیش از حد ورودی
|
41 |
+
df = df.sort_values('TradeValue', ascending=False).head(10)
|
42 |
+
|
43 |
+
# بررسی وجود ستونهای استاندارد
|
44 |
std_cols = ['ptCode', 'ptTitle', 'TradeValue']
|
45 |
if all(col in df.columns for col in std_cols):
|
46 |
out = df[std_cols]
|
47 |
out.columns = ['کد کشور', 'نام کشور', 'ارزش CIF']
|
48 |
return product_name, out
|
49 |
|
50 |
+
# یافتن ستونهای مشابه بر اساس نام
|
51 |
+
code_col = next((c for c in df.columns if 'code' in c.lower()), None)
|
52 |
+
title_col = next((c for c in df.columns if 'title' in c.lower()), None)
|
53 |
+
value_col = next((c for c in df.columns if 'value' in c.lower()), None)
|
|
|
54 |
if code_col and title_col and value_col:
|
55 |
out = df[[code_col, title_col, value_col]]
|
56 |
out.columns = ['کد کشور', 'نام کشور', 'ارزش CIF']
|
57 |
return product_name, out
|
58 |
|
59 |
+
# اگر هیچکدام پیدا نشد، DataFrame خام را برگردان
|
60 |
return product_name, df
|
61 |
|
|
|
62 |
# --- تابع تولید مشاوره تخصصی با GPU کرایهای ---
|
63 |
+
hf_token = os.getenv("HF_API_TOKEN")
|
64 |
+
client = InferenceClient(token=hf_token)
|
65 |
translator = GoogleTranslator(source='en', target='fa')
|
66 |
|
67 |
@spaces.GPU
|
68 |
def provide_advice(table_data: pd.DataFrame, hs_code: str, year: str, month: str):
|
69 |
if table_data is None or table_data.empty:
|
70 |
return "ابتدا نمایش دادههای واردات را انجام دهید."
|
71 |
+
|
72 |
+
# محدودسازی تعداد ردیفها برای متن ورودی
|
73 |
+
df_limited = table_data.head(10)
|
74 |
+
table_str = df_limited.to_string(index=False)
|
75 |
period = f"{year}/{int(month):02d}"
|
76 |
prompt = (
|
77 |
+
f"The following table shows the top 10 countries by CIF value importing HS code {hs_code} during {period}:\n"
|
78 |
f"{table_str}\n\n"
|
79 |
"Please provide a detailed and comprehensive analysis of market trends, risks, "
|
80 |
"and opportunities for a new exporter entering this market."
|
|
|
89 |
except Exception as e:
|
90 |
return f"خطا در تولید مشاوره: {e}"
|
91 |
|
|
|
92 |
# --- رابط کاربری Gradio ---
|
93 |
with gr.Blocks() as demo:
|
94 |
gr.Markdown("## تحلیل واردات بر اساس کد HS و ارائه مشاوره تخصصی")
|
95 |
|
96 |
with gr.Row():
|
97 |
+
inp_hs = gr.Textbox(label="کد HS", placeholder="مثلاً 1006")
|
98 |
+
inp_year = gr.Textbox(label="سال", placeholder="مثلاً 2023")
|
99 |
+
inp_month = gr.Textbox(label="ماه", placeholder="مثلاً 1 تا 12")
|
100 |
|
101 |
+
btn_show = gr.Button("نمایش دادههای واردات")
|
102 |
+
out_name = gr.Markdown(label="**نام محصول**")
|
103 |
out_table = gr.Dataframe(
|
104 |
datatype="pandas",
|
105 |
interactive=True
|