diginoron commited on
Commit
0490f39
·
verified ·
1 Parent(s): 3caec61

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -11
app.py CHANGED
@@ -2,13 +2,14 @@ import os
2
  import gradio as gr
3
  import pandas as pd
4
  import comtradeapicall
5
- from huggingface_hub import InferenceApi
6
 
 
7
  subscription_key = os.getenv("COMTRADE_API_KEY", "")
8
- hf_token = os.getenv("HF_API_TOKEN", None)
9
- proxy_url = None
10
 
11
- inference = InferenceApi(repo_id="google/gemma-2b", token=hf_token)
12
 
13
  def get_importers(hs_code: str, year: str, month: str):
14
  period = f"{year}{int(month):02d}"
@@ -17,8 +18,7 @@ def get_importers(hs_code: str, year: str, month: str):
17
  reporterCode=None, cmdCode=hs_code, flowCode='M',
18
  partnerCode=None, partner2Code=None,
19
  customsCode=None, motCode=None,
20
- maxRecords=500, includeDesc=True,
21
- proxy_url=proxy_url
22
  )
23
  if df is None or df.empty:
24
  return pd.DataFrame(columns=["کد کشور","نام کشور","ارزش CIF"])
@@ -32,7 +32,6 @@ def get_importers(hs_code: str, year: str, month: str):
32
  return result
33
 
34
  def provide_advice(table_data: pd.DataFrame, hs_code: str, year: str, month: str):
35
- # بررسی خالی‌بودن DataFrame
36
  if table_data is None or table_data.empty:
37
  return "ابتدا باید اطلاعات واردات را نمایش دهید."
38
  table_str = table_data.to_string(index=False)
@@ -42,8 +41,14 @@ def provide_advice(table_data: pd.DataFrame, hs_code: str, year: str, month: str
42
  f"{table_str}\n\n"
43
  "لطفاً بر اساس این اطلاعات دو پاراگراف مشاوره تخصصی بنویسید."
44
  )
45
- raw = inference(prompt, raw_response=True)
46
- return raw.text
 
 
 
 
 
 
47
 
48
  current_year = pd.Timestamp.now().year
49
  years = [str(y) for y in range(2000, current_year+1)]
@@ -61,10 +66,10 @@ with gr.Blocks() as demo:
61
  datatype=["number","text","number"],
62
  interactive=True,
63
  )
64
- btn_show.click(get_importers, inputs=[inp_hs, inp_year, inp_month], outputs=out_table)
65
 
66
  btn_advice = gr.Button("ارائه مشاوره تخصصی")
67
- out_advice = gr.Textbox(label="مشاوره تخصصی", lines=5)
68
 
69
  btn_advice.click(
70
  provide_advice,
 
2
  import gradio as gr
3
  import pandas as pd
4
  import comtradeapicall
5
+ from huggingface_hub import InferenceClient
6
 
7
+ # کلید COMTRADE (فعلاً برای preview نیازی به آن نداریم)
8
  subscription_key = os.getenv("COMTRADE_API_KEY", "")
9
+ # توکن Hugging Face
10
+ hf_token = os.getenv("HF_API_TOKEN")
11
 
12
+ client = InferenceClient(token=hf_token)
13
 
14
  def get_importers(hs_code: str, year: str, month: str):
15
  period = f"{year}{int(month):02d}"
 
18
  reporterCode=None, cmdCode=hs_code, flowCode='M',
19
  partnerCode=None, partner2Code=None,
20
  customsCode=None, motCode=None,
21
+ maxRecords=500, includeDesc=True
 
22
  )
23
  if df is None or df.empty:
24
  return pd.DataFrame(columns=["کد کشور","نام کشور","ارزش CIF"])
 
32
  return result
33
 
34
  def provide_advice(table_data: pd.DataFrame, hs_code: str, year: str, month: str):
 
35
  if table_data is None or table_data.empty:
36
  return "ابتدا باید اطلاعات واردات را نمایش دهید."
37
  table_str = table_data.to_string(index=False)
 
41
  f"{table_str}\n\n"
42
  "لطفاً بر اساس این اطلاعات دو پاراگراف مشاوره تخصصی بنویسید."
43
  )
44
+ # فراخوانی text_generation
45
+ outputs = client.text_generation(
46
+ model="google/gemma-2b",
47
+ inputs=prompt,
48
+ parameters={"max_new_tokens":256}
49
+ )
50
+ # خروجی اولین شات
51
+ return outputs[0]["generated_text"]
52
 
53
  current_year = pd.Timestamp.now().year
54
  years = [str(y) for y in range(2000, current_year+1)]
 
66
  datatype=["number","text","number"],
67
  interactive=True,
68
  )
69
+ btn_show.click(get_importers, [inp_hs, inp_year, inp_month], out_table)
70
 
71
  btn_advice = gr.Button("ارائه مشاوره تخصصی")
72
+ out_advice = gr.Textbox(label="مشاوره تخصصی", lines=6)
73
 
74
  btn_advice.click(
75
  provide_advice,