Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,8 +5,7 @@ import gradio as gr
|
|
5 |
import comtradeapicall
|
6 |
from openai import OpenAI
|
7 |
from deep_translator import GoogleTranslator
|
8 |
-
import
|
9 |
-
import spaces
|
10 |
|
11 |
# --- بارگذاری دادههای HS Code ---
|
12 |
HS_CSV_URL = (
|
@@ -50,22 +49,8 @@ def get_importers(hs_code: str, year: str, month: str):
|
|
50 |
out.columns = ['کد کشور', 'نام کشور', 'ارزش CIF']
|
51 |
return product_name, out
|
52 |
|
53 |
-
# ---
|
54 |
-
|
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 |
|
71 |
@spaces.GPU
|
@@ -108,24 +93,15 @@ with gr.Blocks() as demo:
|
|
108 |
inp_month = gr.Textbox(label="ماه", placeholder="مثلاً 1 تا 12")
|
109 |
|
110 |
btn_show = gr.Button("نمایش دادههای واردات")
|
111 |
-
|
112 |
out_name = gr.Markdown(label="**نام محصول**")
|
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=
|
124 |
inputs=[inp_hs, inp_year, inp_month],
|
125 |
-
outputs=[out_name, out_table
|
126 |
)
|
127 |
|
128 |
-
# دکمه دوم: مشاوره تخصصی
|
129 |
btn_advice = gr.Button("ارائه مشاوره تخصصی")
|
130 |
out_advice = gr.Textbox(label="مشاوره تخصصی", lines=8)
|
131 |
|
|
|
5 |
import comtradeapicall
|
6 |
from openai import OpenAI
|
7 |
from deep_translator import GoogleTranslator
|
8 |
+
import spaces # برای مدیریت GPU کرایهای
|
|
|
9 |
|
10 |
# --- بارگذاری دادههای HS Code ---
|
11 |
HS_CSV_URL = (
|
|
|
49 |
out.columns = ['کد کشور', 'نام کشور', 'ارزش CIF']
|
50 |
return product_name, out
|
51 |
|
52 |
+
# --- اتصال به OpenAI و مترجم ---
|
53 |
+
openai_client = OpenAI(api_key=os.getenv("OPENAI")) # سکرت از محیط
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
translator = GoogleTranslator(source='en', target='fa')
|
55 |
|
56 |
@spaces.GPU
|
|
|
93 |
inp_month = gr.Textbox(label="ماه", placeholder="مثلاً 1 تا 12")
|
94 |
|
95 |
btn_show = gr.Button("نمایش دادههای واردات")
|
|
|
96 |
out_name = gr.Markdown(label="**نام محصول**")
|
97 |
out_table = gr.Dataframe(datatype="pandas", interactive=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
|
99 |
btn_show.click(
|
100 |
+
fn=get_importers,
|
101 |
inputs=[inp_hs, inp_year, inp_month],
|
102 |
+
outputs=[out_name, out_table]
|
103 |
)
|
104 |
|
|
|
105 |
btn_advice = gr.Button("ارائه مشاوره تخصصی")
|
106 |
out_advice = gr.Textbox(label="مشاوره تخصصی", lines=8)
|
107 |
|