Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,7 +6,7 @@ import comtradeapicall
|
|
6 |
from openai import OpenAI
|
7 |
from deep_translator import GoogleTranslator
|
8 |
import matplotlib.pyplot as plt
|
9 |
-
import spaces
|
10 |
|
11 |
# --- بارگذاری دادههای HS Code ---
|
12 |
HS_CSV_URL = (
|
@@ -50,21 +50,21 @@ def get_importers(hs_code: str, year: str, month: str):
|
|
50 |
out.columns = ['کد کشور', 'نام کشور', 'ارزش CIF']
|
51 |
return product_name, out
|
52 |
|
53 |
-
# --- ترسیم نمودار ۵ کشور
|
54 |
def plot_top5_chart(table_data: pd.DataFrame):
|
55 |
if table_data is None or table_data.empty:
|
56 |
return None
|
57 |
|
58 |
df_sorted = table_data.sort_values("ارزش CIF", ascending=False).head(5)
|
59 |
-
|
60 |
-
fig, ax = plt.subplots()
|
61 |
ax.bar(df_sorted["نام کشور"], df_sorted["ارزش CIF"], color="skyblue")
|
62 |
ax.set_ylabel("ارزش CIF (USD)")
|
63 |
ax.set_title("۵ کشور اول واردکننده")
|
64 |
ax.tick_params(axis='x', rotation=45)
|
|
|
65 |
return fig
|
66 |
|
67 |
-
# ---
|
68 |
openai_client = OpenAI(api_key=os.getenv("OPENAI"))
|
69 |
translator = GoogleTranslator(source='en', target='fa')
|
70 |
|
@@ -113,18 +113,19 @@ with gr.Blocks() as demo:
|
|
113 |
out_table = gr.Dataframe(datatype="pandas", interactive=True)
|
114 |
out_chart = gr.Plot(label="نمودار ۵ کشور اول واردکننده")
|
115 |
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
|
122 |
btn_show.click(
|
123 |
-
fn=
|
124 |
-
inputs=[
|
125 |
-
outputs=[out_chart]
|
126 |
)
|
127 |
|
|
|
128 |
btn_advice = gr.Button("ارائه مشاوره تخصصی")
|
129 |
out_advice = gr.Textbox(label="مشاوره تخصصی", lines=8)
|
130 |
|
|
|
6 |
from openai import OpenAI
|
7 |
from deep_translator import GoogleTranslator
|
8 |
import matplotlib.pyplot as plt
|
9 |
+
import spaces
|
10 |
|
11 |
# --- بارگذاری دادههای HS Code ---
|
12 |
HS_CSV_URL = (
|
|
|
50 |
out.columns = ['کد کشور', 'نام کشور', 'ارزش CIF']
|
51 |
return product_name, out
|
52 |
|
53 |
+
# --- تابع ترسیم نمودار ۵ کشور برتر ---
|
54 |
def plot_top5_chart(table_data: pd.DataFrame):
|
55 |
if table_data is None or table_data.empty:
|
56 |
return None
|
57 |
|
58 |
df_sorted = table_data.sort_values("ارزش CIF", ascending=False).head(5)
|
59 |
+
fig, ax = plt.subplots(figsize=(6, 4))
|
|
|
60 |
ax.bar(df_sorted["نام کشور"], df_sorted["ارزش CIF"], color="skyblue")
|
61 |
ax.set_ylabel("ارزش CIF (USD)")
|
62 |
ax.set_title("۵ کشور اول واردکننده")
|
63 |
ax.tick_params(axis='x', rotation=45)
|
64 |
+
plt.tight_layout()
|
65 |
return fig
|
66 |
|
67 |
+
# --- مشاوره با GPT + ترجمه فارسی ---
|
68 |
openai_client = OpenAI(api_key=os.getenv("OPENAI"))
|
69 |
translator = GoogleTranslator(source='en', target='fa')
|
70 |
|
|
|
113 |
out_table = gr.Dataframe(datatype="pandas", interactive=True)
|
114 |
out_chart = gr.Plot(label="نمودار ۵ کشور اول واردکننده")
|
115 |
|
116 |
+
# دکمه اول: دو خروجی با هم
|
117 |
+
def combined_output(hs_code, year, month):
|
118 |
+
name, table = get_importers(hs_code, year, month)
|
119 |
+
chart = plot_top5_chart(table)
|
120 |
+
return name, table, chart
|
121 |
|
122 |
btn_show.click(
|
123 |
+
fn=combined_output,
|
124 |
+
inputs=[inp_hs, inp_year, inp_month],
|
125 |
+
outputs=[out_name, out_table, out_chart]
|
126 |
)
|
127 |
|
128 |
+
# دکمه دوم: مشاوره تخصصی
|
129 |
btn_advice = gr.Button("ارائه مشاوره تخصصی")
|
130 |
out_advice = gr.Textbox(label="مشاوره تخصصی", lines=8)
|
131 |
|