diginoron commited on
Commit
6f5b134
·
verified ·
1 Parent(s): d0f7177

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -13
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 # برای GPU کرایه‌ای
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
- # --- اتصال به OpenAI و مترجم ---
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
- btn_show.click(
117
- fn=get_importers,
118
- inputs=[inp_hs, inp_year, inp_month],
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
 
 
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