Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,21 +1,20 @@
|
|
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
import pandas as pd
|
4 |
import comtradeapicall
|
5 |
from huggingface_hub import InferenceClient
|
6 |
from deep_translator import GoogleTranslator
|
7 |
-
|
8 |
|
9 |
# کلید COMTRADE
|
10 |
subscription_key = os.getenv("COMTRADE_API_KEY", "")
|
11 |
# توکن Hugging Face
|
12 |
hf_token = os.getenv("HF_API_TOKEN")
|
13 |
|
14 |
-
|
15 |
client = InferenceClient(token=hf_token)
|
16 |
translator = GoogleTranslator(source='en', target='fa')
|
17 |
|
18 |
-
|
19 |
def get_importers(hs_code: str, year: str, month: str):
|
20 |
period = f"{year}{int(month):02d}"
|
21 |
df = comtradeapicall.previewFinalData(
|
@@ -26,17 +25,13 @@ def get_importers(hs_code: str, year: str, month: str):
|
|
26 |
maxRecords=500, includeDesc=True
|
27 |
)
|
28 |
if df is None or df.empty:
|
29 |
-
return pd.DataFrame(
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
.sort_values("cifvalue", ascending=False)
|
35 |
-
)
|
36 |
-
result.columns = ["کد کشور", "نام کشور", "ارزش CIF"]
|
37 |
-
return result
|
38 |
-
|
39 |
|
|
|
40 |
def provide_advice(table_data: pd.DataFrame, hs_code: str, year: str, month: str):
|
41 |
if table_data is None or table_data.empty:
|
42 |
return "ابتدا باید اطلاعات واردات را نمایش دهید."
|
@@ -45,22 +40,21 @@ def provide_advice(table_data: pd.DataFrame, hs_code: str, year: str, month: str
|
|
45 |
prompt = (
|
46 |
f"The following table shows countries that imported a product with HS code {hs_code} during the period {period}:\n"
|
47 |
f"{table_str}\n\n"
|
48 |
-
|
|
|
|
|
|
|
49 |
)
|
50 |
-
print("پرامپت ساختهشده:")
|
51 |
-
print(prompt)
|
52 |
try:
|
53 |
print("در حال فراخوانی مدل mistralai/Mixtral-8x7B-Instruct-v0.1...")
|
54 |
outputs = client.text_generation(
|
55 |
prompt=prompt,
|
56 |
model="mistralai/Mixtral-8x7B-Instruct-v0.1",
|
57 |
-
max_new_tokens=1024
|
58 |
)
|
59 |
print("خروجی مدل دریافت شد (به انگلیسی):")
|
60 |
print(outputs)
|
61 |
|
62 |
-
|
63 |
-
# ترجمه خروجی به فارسی
|
64 |
translated_outputs = translator.translate(outputs)
|
65 |
print("خروجی ترجمهشده به فارسی:")
|
66 |
print(translated_outputs)
|
@@ -70,19 +64,14 @@ def provide_advice(table_data: pd.DataFrame, hs_code: str, year: str, month: str
|
|
70 |
print(error_msg)
|
71 |
return error_msg
|
72 |
|
73 |
-
|
74 |
-
current_year = pd.Timestamp.now().year
|
75 |
-
years = [str(y) for y in range(2000, current_year+1)]
|
76 |
-
months = [str(m) for m in range(1, 13)]
|
77 |
-
|
78 |
-
|
79 |
with gr.Blocks() as demo:
|
80 |
-
gr.Markdown("
|
|
|
81 |
with gr.Row():
|
82 |
-
inp_hs = gr.Textbox(label="HS
|
83 |
-
inp_year = gr.
|
84 |
-
inp_month = gr.
|
85 |
-
btn_show = gr.Button("نمایش
|
86 |
out_table = gr.Dataframe(
|
87 |
headers=["کد کشور", "نام کشور", "ارزش CIF"],
|
88 |
datatype=["number", "text", "number"],
|
@@ -90,17 +79,13 @@ with gr.Blocks() as demo:
|
|
90 |
)
|
91 |
btn_show.click(get_importers, [inp_hs, inp_year, inp_month], out_table)
|
92 |
|
93 |
-
|
94 |
btn_advice = gr.Button("ارائه مشاوره تخصصی")
|
95 |
out_advice = gr.Textbox(label="مشاوره تخصصی", lines=6)
|
96 |
-
|
97 |
-
|
98 |
btn_advice.click(
|
99 |
provide_advice,
|
100 |
inputs=[out_table, inp_hs, inp_year, inp_month],
|
101 |
outputs=out_advice
|
102 |
)
|
103 |
|
104 |
-
|
105 |
if __name__ == "__main__":
|
106 |
demo.launch()
|
|
|
1 |
+
# app.py
|
2 |
import os
|
3 |
import gradio as gr
|
4 |
import pandas as pd
|
5 |
import comtradeapicall
|
6 |
from huggingface_hub import InferenceClient
|
7 |
from deep_translator import GoogleTranslator
|
8 |
+
import spaces # اضافهشده برای مدیریت GPU روی ZeroGPU Spaces
|
9 |
|
10 |
# کلید COMTRADE
|
11 |
subscription_key = os.getenv("COMTRADE_API_KEY", "")
|
12 |
# توکن Hugging Face
|
13 |
hf_token = os.getenv("HF_API_TOKEN")
|
14 |
|
|
|
15 |
client = InferenceClient(token=hf_token)
|
16 |
translator = GoogleTranslator(source='en', target='fa')
|
17 |
|
|
|
18 |
def get_importers(hs_code: str, year: str, month: str):
|
19 |
period = f"{year}{int(month):02d}"
|
20 |
df = comtradeapicall.previewFinalData(
|
|
|
25 |
maxRecords=500, includeDesc=True
|
26 |
)
|
27 |
if df is None or df.empty:
|
28 |
+
return pd.DataFrame() # خالی
|
29 |
+
# فقط ستونهای مورد نیاز را نگهدار
|
30 |
+
df = df[['ptCode', 'ptTitle', 'TradeValue']]
|
31 |
+
df.columns = ['کد کشور', 'نام کشور', 'ارزش CIF']
|
32 |
+
return df
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
+
@spaces.GPU
|
35 |
def provide_advice(table_data: pd.DataFrame, hs_code: str, year: str, month: str):
|
36 |
if table_data is None or table_data.empty:
|
37 |
return "ابتدا باید اطلاعات واردات را نمایش دهید."
|
|
|
40 |
prompt = (
|
41 |
f"The following table shows countries that imported a product with HS code {hs_code} during the period {period}:\n"
|
42 |
f"{table_str}\n\n"
|
43 |
+
"Please provide a detailed and comprehensive analysis of market trends, "
|
44 |
+
"risks, and opportunities for a new exporter entering this market. "
|
45 |
+
"Include competitive landscape, pricing benchmarks, logistical considerations, "
|
46 |
+
"risk management techniques, and steps to establish local partnerships."
|
47 |
)
|
|
|
|
|
48 |
try:
|
49 |
print("در حال فراخوانی مدل mistralai/Mixtral-8x7B-Instruct-v0.1...")
|
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)
|
57 |
|
|
|
|
|
58 |
translated_outputs = translator.translate(outputs)
|
59 |
print("خروجی ترجمهشده به فارسی:")
|
60 |
print(translated_outputs)
|
|
|
64 |
print(error_msg)
|
65 |
return error_msg
|
66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
with gr.Blocks() as demo:
|
68 |
+
gr.Markdown("## تحلیل واردات بر اساس کد HS و ارائه مشاوره تخصصی")
|
69 |
+
|
70 |
with gr.Row():
|
71 |
+
inp_hs = gr.Textbox(label="کد HS", placeholder="مثلاً 100610")
|
72 |
+
inp_year = gr.Textbox(label="سال", placeholder="مثلاً 2023")
|
73 |
+
inp_month = gr.Textbox(label="ماه", placeholder="مثلاً 1 تا 12")
|
74 |
+
btn_show = gr.Button("نمایش واردات")
|
75 |
out_table = gr.Dataframe(
|
76 |
headers=["کد کشور", "نام کشور", "ارزش CIF"],
|
77 |
datatype=["number", "text", "number"],
|
|
|
79 |
)
|
80 |
btn_show.click(get_importers, [inp_hs, inp_year, inp_month], out_table)
|
81 |
|
|
|
82 |
btn_advice = gr.Button("ارائه مشاوره تخصصی")
|
83 |
out_advice = gr.Textbox(label="مشاوره تخصصی", lines=6)
|
|
|
|
|
84 |
btn_advice.click(
|
85 |
provide_advice,
|
86 |
inputs=[out_table, inp_hs, inp_year, inp_month],
|
87 |
outputs=out_advice
|
88 |
)
|
89 |
|
|
|
90 |
if __name__ == "__main__":
|
91 |
demo.launch()
|