diginoron commited on
Commit
b984707
·
verified ·
1 Parent(s): 39bcd35

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -2
app.py CHANGED
@@ -5,7 +5,8 @@ import gradio as gr
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,8 +50,22 @@ def get_importers(hs_code: str, year: str, month: str):
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,8 +108,10 @@ with gr.Blocks() as demo:
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,
@@ -102,6 +119,12 @@ with gr.Blocks() as demo:
102
  outputs=[out_name, out_table]
103
  )
104
 
 
 
 
 
 
 
105
  btn_advice = gr.Button("ارائه مشاوره تخصصی")
106
  out_advice = gr.Textbox(label="مشاوره تخصصی", lines=8)
107
 
 
5
  import comtradeapicall
6
  from openai import OpenAI
7
  from deep_translator import GoogleTranslator
8
+ import matplotlib.pyplot as plt
9
+ import spaces # برای GPU کرایه‌ای
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
+
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
  # --- اتصال به OpenAI و مترجم ---
68
+ openai_client = OpenAI(api_key=os.getenv("OPENAI"))
69
  translator = GoogleTranslator(source='en', target='fa')
70
 
71
  @spaces.GPU
 
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
  btn_show.click(
117
  fn=get_importers,
 
119
  outputs=[out_name, out_table]
120
  )
121
 
122
+ btn_show.click(
123
+ fn=plot_top5_chart,
124
+ inputs=[out_table],
125
+ outputs=[out_chart]
126
+ )
127
+
128
  btn_advice = gr.Button("ارائه مشاوره تخصصی")
129
  out_advice = gr.Textbox(label="مشاوره تخصصی", lines=8)
130