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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -5
app.py CHANGED
@@ -3,7 +3,7 @@ import os
3
  import pandas as pd
4
  import gradio as gr
5
  import comtradeapicall
6
- import openai
7
  from deep_translator import GoogleTranslator
8
  import spaces # برای مدیریت GPU کرایه‌ای
9
 
@@ -49,8 +49,8 @@ def get_importers(hs_code: str, year: str, month: str):
49
  out.columns = ['کد کشور', 'نام کشور', 'ارزش CIF']
50
  return product_name, out
51
 
52
- # --- اتصال به OpenAI با کلید سکرت HuggingFace ---
53
- openai.api_key = os.getenv("OPENAI") # استفاده از سکرت تعریف‌شده در فضای HF
54
  translator = GoogleTranslator(source='en', target='fa')
55
 
56
  @spaces.GPU
@@ -69,7 +69,7 @@ def provide_advice(table_data: pd.DataFrame, hs_code: str, year: str, month: str
69
  )
70
 
71
  try:
72
- response = openai.ChatCompletion.create(
73
  model="gpt-3.5-turbo",
74
  messages=[
75
  {"role": "system", "content": "You are an expert in international trade and export consulting."},
@@ -78,7 +78,7 @@ def provide_advice(table_data: pd.DataFrame, hs_code: str, year: str, month: str
78
  max_tokens=1000,
79
  temperature=0.7
80
  )
81
- english_response = response['choices'][0]['message']['content']
82
  return translator.translate(english_response)
83
  except Exception as e:
84
  return f"خطا در تولید مشاوره: {e}"
 
3
  import pandas as pd
4
  import gradio as gr
5
  import comtradeapicall
6
+ from openai import OpenAI
7
  from deep_translator import GoogleTranslator
8
  import spaces # برای مدیریت GPU کرایه‌ای
9
 
 
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
 
69
  )
70
 
71
  try:
72
+ response = openai_client.chat.completions.create(
73
  model="gpt-3.5-turbo",
74
  messages=[
75
  {"role": "system", "content": "You are an expert in international trade and export consulting."},
 
78
  max_tokens=1000,
79
  temperature=0.7
80
  )
81
+ english_response = response.choices[0].message.content
82
  return translator.translate(english_response)
83
  except Exception as e:
84
  return f"خطا در تولید مشاوره: {e}"