myn0908 commited on
Commit
77ed5d2
·
1 Parent(s): ea340dc

handle api key for save cost limit api

Browse files
LanguageTranslator/model/openai_model.py CHANGED
@@ -20,7 +20,7 @@ class Model:
20
  class OpenAIModel(Model):
21
  def __init__(self, model_name: str, api_key: str):
22
  self.model_name = model_name
23
- openai.api_key = api_key
24
 
25
  def make_translation_request(self, prompt):
26
  attempts = 0
 
20
  class OpenAIModel(Model):
21
  def __init__(self, model_name: str, api_key: str):
22
  self.model_name = model_name
23
+ # openai.api_key = api_key
24
 
25
  def make_translation_request(self, prompt):
26
  attempts = 0
LanguageTranslator/utils/constants.py ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import openai
3
+
4
+
5
+ def set_api_key(api_key):
6
+ openai.api_key = api_key
7
+ return api_key
8
+
9
+
10
+ def stop_api_key(value):
11
+ os.environ.pop('OPENAI_API_KEY', None)
12
+ openai.api_key = None
13
+ return 'Stopped API key'
api.py CHANGED
@@ -3,6 +3,7 @@ from starlette.responses import RedirectResponse
3
  from typing import Union, List
4
  from pydantic import BaseModel
5
  from translator import ServerTranslator
 
6
  import json
7
  import uvicorn
8
 
@@ -28,13 +29,16 @@ async def index():
28
  @app.post("/translate", response_model=TranslationResult)
29
  async def run_translation_manual(
30
  text: str = Query(..., description="Input text to translate"),
31
- dest_language: str = Query(..., description="Destination language")):
 
 
32
  # Splitting the input text
33
  text = text.split(',')
34
  # Creating and processing the translator
35
  processing_language = ServerTranslator.language_translator(
36
  text=text,
37
  dest_language=dest_language,
 
38
  )
39
  # Getting the translated result
40
  result_response = processing_language.translate()
@@ -42,14 +46,16 @@ async def run_translation_manual(
42
 
43
 
44
  @app.post("/translate_json", response_model=TranslationResult)
45
- async def run_translation_auto(json_file: UploadFile):
46
  try:
 
47
  # Reading the JSON content from the file
48
  json_content = await json_file.read()
49
  json_data = json.loads(json_content.decode("utf-8"))
50
  # Creating and processing the translator
51
  processing_language = ServerTranslator.language_translator(
52
- json_data
 
53
  )
54
  # Getting the translated result
55
  result_response = processing_language.translate()
 
3
  from typing import Union, List
4
  from pydantic import BaseModel
5
  from translator import ServerTranslator
6
+ from LanguageTranslator.utils.constants import set_api_key
7
  import json
8
  import uvicorn
9
 
 
29
  @app.post("/translate", response_model=TranslationResult)
30
  async def run_translation_manual(
31
  text: str = Query(..., description="Input text to translate"),
32
+ dest_language: str = Query(..., description="Destination language"),
33
+ api_key: str = Query(..., description="OpenAI API Key")):
34
+ set_api_key(api_key)
35
  # Splitting the input text
36
  text = text.split(',')
37
  # Creating and processing the translator
38
  processing_language = ServerTranslator.language_translator(
39
  text=text,
40
  dest_language=dest_language,
41
+ openai_api_key=api_key
42
  )
43
  # Getting the translated result
44
  result_response = processing_language.translate()
 
46
 
47
 
48
  @app.post("/translate_json", response_model=TranslationResult)
49
+ async def run_translation_auto(json_file: UploadFile, api_key: str = Query(..., description="OpenAI API Key")):
50
  try:
51
+ set_api_key(api_key)
52
  # Reading the JSON content from the file
53
  json_content = await json_file.read()
54
  json_data = json.loads(json_content.decode("utf-8"))
55
  # Creating and processing the translator
56
  processing_language = ServerTranslator.language_translator(
57
+ json_data,
58
+ openai_api_key=api_key
59
  )
60
  # Getting the translated result
61
  result_response = processing_language.translate()
app.py CHANGED
@@ -1,8 +1,14 @@
1
  import gradio as gr
2
  from translator import ServerTranslator
 
3
  import json
4
 
5
 
 
 
 
 
 
6
  def clear_all(input_json, input_text, input_dest_lang, translated_text_json, translted_text_text):
7
  return None, "", "", "", ""
8
 
@@ -63,5 +69,22 @@ with gr.Blocks() as demo:
63
  [input_json, input_text, input_dest_lang, translated_text_json, translated_text_text],
64
  [input_json, input_text, input_dest_lang, translated_text_json, translated_text_text]
65
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
  demo.launch(share=True)
 
1
  import gradio as gr
2
  from translator import ServerTranslator
3
+ from LanguageTranslator.utils.constants import set_api_key, stop_api_key
4
  import json
5
 
6
 
7
+ def apply_api_key(api_key):
8
+ api_key = set_api_key(api_key=api_key)
9
+ return f'Successfully set {api_key}'
10
+
11
+
12
  def clear_all(input_json, input_text, input_dest_lang, translated_text_json, translted_text_text):
13
  return None, "", "", "", ""
14
 
 
69
  [input_json, input_text, input_dest_lang, translated_text_json, translated_text_text],
70
  [input_json, input_text, input_dest_lang, translated_text_json, translated_text_text]
71
  )
72
+ with gr.Row():
73
+ with gr.Column():
74
+ status = gr.Textbox(label='API KEY STATUS')
75
+ api_key_set = gr.Textbox(label='Set your OPENAI API KEY')
76
+ api_key_set_button = gr.Button("Set API key")
77
+ api_key_set_button.click(
78
+ apply_api_key,
79
+ [api_key_set],
80
+ [status]
81
+ )
82
+ with gr.Column():
83
+ status_2 = gr.Textbox(label='STOP API KEY STATUS')
84
+ stop_api_button = gr.Button('Stop API key')
85
+ stop_api_button.click(
86
+ stop_api_key,
87
+ [],
88
+ [status_2])
89
 
90
  demo.launch(share=True)