diginoron commited on
Commit
de72ef1
·
verified ·
1 Parent(s): b975240

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -11
app.py CHANGED
@@ -20,10 +20,10 @@ def get_importers(hs_code: str, year: str, month: str):
20
  reporterCode=None, cmdCode=hs_code, flowCode='M',
21
  partnerCode=None, partner2Code=None,
22
  customsCode=None, motCode=None,
23
- maxRecords=500, includeDesc=True
24
  )
25
  if df is None or df.empty:
26
- return pd.DataFrame(columns=["کد کشور", "نام کشور", "ارزش CIF"])
27
  df = df[df['cifvalue'] > 0]
28
  result = (
29
  df.groupby(["reporterCode", "reporterDesc"], as_index=False)
@@ -31,17 +31,20 @@ def get_importers(hs_code: str, year: str, month: str):
31
  .sort_values("cifvalue", ascending=False)
32
  )
33
  result.columns = ["کد کشور", "نام کشور", "ارزش CIF"]
34
- return result
 
 
 
35
 
36
- def provide_advice(table_data: pd.DataFrame, hs_code: str, year: str, month: str):
37
  if table_data is None or table_data.empty:
38
  return "ابتدا باید اطلاعات واردات را نمایش دهید."
39
  table_str = table_data.to_string(index=False)
40
  period = f"{year}/{int(month):02d}"
41
  prompt = (
42
- f"The following table shows countries that imported a product with HS code {hs_code} during the period {period}:\n"
43
  f"{table_str}\n\n"
44
- f"Please provide a detailed and comprehensive analysis in two paragraphs. The first paragraph should discuss market opportunities, potential demand, and specific cultural or economic factors influencing the demand for this product in these countries. The second paragraph should offer actionable strategic recommendations for exporters, including detailed trade strategies, risk management techniques, and steps to establish local partnerships."
45
  )
46
  print("پرامپت ساخته‌شده:")
47
  print(prompt)
@@ -50,7 +53,7 @@ def provide_advice(table_data: pd.DataFrame, hs_code: str, year: str, month: str
50
  outputs = client.text_generation(
51
  prompt=prompt,
52
  model="mistralai/Mixtral-8x7B-Instruct-v0.1",
53
- max_new_tokens=1024 # افزایش برای تکمیل جملات
54
  )
55
  print("خروجی مدل دریافت شد (به انگلیسی):")
56
  print(outputs)
@@ -72,7 +75,7 @@ months = [str(m) for m in range(1, 13)]
72
  with gr.Blocks() as demo:
73
  gr.Markdown("## نمایش کشورهایی که یک کالا را وارد کرده‌اند")
74
  with gr.Row():
75
- inp_hs = gr.Textbox(label="HS Code")
76
  inp_year = gr.Dropdown(choices=years, label="سال", value=str(current_year))
77
  inp_month = gr.Dropdown(choices=months, label="ماه", value=str(pd.Timestamp.now().month))
78
  btn_show = gr.Button("نمایش اطلاعات")
@@ -81,14 +84,18 @@ with gr.Blocks() as demo:
81
  datatype=["number", "text", "number"],
82
  interactive=True,
83
  )
84
- btn_show.click(get_importers, [inp_hs, inp_year, inp_month], out_table)
 
 
 
 
85
 
86
  btn_advice = gr.Button("ارائه مشاوره تخصصی")
87
  out_advice = gr.Textbox(label="مشاوره تخصصی", lines=6)
88
 
89
  btn_advice.click(
90
- provide_advice,
91
- inputs=[out_table, inp_hs, inp_year, inp_month],
92
  outputs=out_advice
93
  )
94
 
 
20
  reporterCode=None, cmdCode=hs_code, flowCode='M',
21
  partnerCode=None, partner2Code=None,
22
  customsCode=None, motCode=None,
23
+ maxRecords=500, includeDesc=True # دریافت توضیحات محصول
24
  )
25
  if df is None or df.empty:
26
+ return pd.DataFrame(columns=["کد کشور", "نام کشور", "ارزش CIF"]), "برنج" # پیش‌فرض برنج
27
  df = df[df['cifvalue'] > 0]
28
  result = (
29
  df.groupby(["reporterCode", "reporterDesc"], as_index=False)
 
31
  .sort_values("cifvalue", ascending=False)
32
  )
33
  result.columns = ["کد کشور", "نام کشور", "ارزش CIF"]
34
+
35
+ # استخراج نام محصول از توضیحات (فرض بر این است که cmdDesc نام محصول را دارد)
36
+ product_name = df['cmdDesc'].iloc[0] if 'cmdDesc' in df.columns else "برنج"
37
+ return result, product_name
38
 
39
+ def provide_advice(table_data: pd.DataFrame, hs_code: str, year: str, month: str, product_name: str):
40
  if table_data is None or table_data.empty:
41
  return "ابتدا باید اطلاعات واردات را نمایش دهید."
42
  table_str = table_data.to_string(index=False)
43
  period = f"{year}/{int(month):02d}"
44
  prompt = (
45
+ f"The following table shows countries that imported the product '{product_name}' with HS code {hs_code} during the period {period}:\n"
46
  f"{table_str}\n\n"
47
+ f"لطفاً یک تحلیل کامل ارائه دهید که شامل دو بخش باشد. بخش اول فرصت‌های بازار و تقاضای بالقوه برای این محصول در این کشورها را بررسی کند، با توجه به عوامل فرهنگی، اقتصادی و جمعیتی. بخش دوم توصیه‌های استراتژیک عملی برای صادرکنندگان که این بازارها را هدف قرار داده‌اند، با تمرکز بر استراتژی‌های تجاری، مدیریت ریسک و ایجاد مشارکت‌های محلی، ارائه دهد."
48
  )
49
  print("پرامپت ساخته‌شده:")
50
  print(prompt)
 
53
  outputs = client.text_generation(
54
  prompt=prompt,
55
  model="mistralai/Mixtral-8x7B-Instruct-v0.1",
56
+ max_new_tokens=1024
57
  )
58
  print("خروجی مدل دریافت شد (به انگلیسی):")
59
  print(outputs)
 
75
  with gr.Blocks() as demo:
76
  gr.Markdown("## نمایش کشورهایی که یک کالا را وارد کرده‌اند")
77
  with gr.Row():
78
+ inp_hs = gr.Textbox(label="HS Code", value="1006") # پیش‌فرض 1006 برای برنج
79
  inp_year = gr.Dropdown(choices=years, label="سال", value=str(current_year))
80
  inp_month = gr.Dropdown(choices=months, label="ماه", value=str(pd.Timestamp.now().month))
81
  btn_show = gr.Button("نمایش اطلاعات")
 
84
  datatype=["number", "text", "number"],
85
  interactive=True,
86
  )
87
+ btn_show.click(
88
+ fn=get_importers,
89
+ inputs=[inp_hs, inp_year, inp_month],
90
+ outputs=[out_table, gr.State()] # gr.State برای انتقال product_name
91
+ )
92
 
93
  btn_advice = gr.Button("ارائه مشاوره تخصصی")
94
  out_advice = gr.Textbox(label="مشاوره تخصصی", lines=6)
95
 
96
  btn_advice.click(
97
+ fn=provide_advice,
98
+ inputs=[out_table, inp_hs, inp_year, inp_month, gr.State()],
99
  outputs=out_advice
100
  )
101