SG34 commited on
Commit
db963a4
·
verified ·
1 Parent(s): bbaf62c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -10
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
 
3
  import os
4
  import random
5
  import logging
@@ -18,7 +19,8 @@ MODELS = {
18
  "Mixtral 8x7B": "mistralai/Mistral-7B-Instruct-v0.3",
19
  "Mixtral Nous-Hermes": "NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO",
20
  "Cohere Command R+": "CohereForAI/c4ai-command-r-plus",
21
- "Aya-23-35B": "CohereForAI/aya-23-35B"
 
22
  }
23
 
24
  # HuggingFace 토큰 설정
@@ -26,22 +28,50 @@ hf_token = os.getenv("HF_TOKEN")
26
  if not hf_token:
27
  raise ValueError("HF_TOKEN 환경 변수가 설정되지 않았습니다.")
28
 
 
 
 
 
 
29
  def call_hf_api(prompt, reference_text, max_tokens, temperature, top_p, model):
30
- client = InferenceClient(model=model, token=hf_token)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  combined_prompt = f"{prompt}\n\n참고 텍스트:\n{reference_text}"
32
- random_seed = random.randint(0, 1000000)
33
 
34
  try:
35
- response = client.text_generation(
36
- combined_prompt,
37
- max_new_tokens=max_tokens,
 
 
 
 
38
  temperature=temperature,
39
  top_p=top_p,
40
- seed=random_seed
41
  )
42
- return response
43
  except Exception as e:
44
- logging.error(f"HuggingFace API 호출 중 오류 발생: {str(e)}")
45
  return f"응답 생성 중 오류 발생: {str(e)}. 나중에 다시 시도해 주세요."
46
 
47
  def generate_response(prompt, reference_text, max_tokens, temperature, top_p, model):
@@ -79,4 +109,4 @@ with gr.Blocks() as demo:
79
  )
80
 
81
  # 인터페이스 실행
82
- demo.launch(share=True)
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
+ import openai # OpenAI 라이브러리 추가
4
  import os
5
  import random
6
  import logging
 
19
  "Mixtral 8x7B": "mistralai/Mistral-7B-Instruct-v0.3",
20
  "Mixtral Nous-Hermes": "NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO",
21
  "Cohere Command R+": "CohereForAI/c4ai-command-r-plus",
22
+ "Aya-23-35B": "CohereForAI/aya-23-35B",
23
+ "GPT-4o Mini": "openai/gpt-4o-mini" # GPT-4o Mini 모델 추가
24
  }
25
 
26
  # HuggingFace 토큰 설정
 
28
  if not hf_token:
29
  raise ValueError("HF_TOKEN 환경 변수가 설정되지 않았습니다.")
30
 
31
+ # OpenAI API 키 설정
32
+ openai.api_key = os.getenv("OPENAI_API_KEY")
33
+ if not openai.api_key:
34
+ raise ValueError("OPENAI_API_KEY 환경 변수가 설정되지 않았습니다.")
35
+
36
  def call_hf_api(prompt, reference_text, max_tokens, temperature, top_p, model):
37
+ if model == "openai/gpt-4o-mini":
38
+ return call_openai_api(prompt, reference_text, max_tokens, temperature, top_p)
39
+ else:
40
+ client = InferenceClient(model=model, token=hf_token)
41
+ combined_prompt = f"{prompt}\n\n참고 텍스트:\n{reference_text}"
42
+ random_seed = random.randint(0, 1000000)
43
+
44
+ try:
45
+ response = client.text_generation(
46
+ combined_prompt,
47
+ max_new_tokens=max_tokens,
48
+ temperature=temperature,
49
+ top_p=top_p,
50
+ seed=random_seed
51
+ )
52
+ return response
53
+ except Exception as e:
54
+ logging.error(f"HuggingFace API 호출 중 오류 발생: {str(e)}")
55
+ return f"응답 생성 중 오류 발생: {str(e)}. 나중에 다시 시도해 주세요."
56
+
57
+ def call_openai_api(prompt, reference_text, max_tokens, temperature, top_p):
58
+ system_message = "You are a helpful assistant."
59
  combined_prompt = f"{prompt}\n\n참고 텍스트:\n{reference_text}"
 
60
 
61
  try:
62
+ response = openai.ChatCompletion.create(
63
+ model="gpt-4o-mini",
64
+ messages=[
65
+ {"role": "system", "content": system_message},
66
+ {"role": "user", "content": combined_prompt},
67
+ ],
68
+ max_tokens=max_tokens,
69
  temperature=temperature,
70
  top_p=top_p,
 
71
  )
72
+ return response.choices[0].message['content']
73
  except Exception as e:
74
+ logging.error(f"OpenAI API 호출 중 오류 발생: {str(e)}")
75
  return f"응답 생성 중 오류 발생: {str(e)}. 나중에 다시 시도해 주세요."
76
 
77
  def generate_response(prompt, reference_text, max_tokens, temperature, top_p, model):
 
109
  )
110
 
111
  # 인터페이스 실행
112
+ demo.launch(share=True)