aliceblue11 commited on
Commit
02b5772
·
verified ·
1 Parent(s): ab1c720

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -2
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  import os
 
4
 
5
  MODELS = {
6
  "Zephyr 7B Beta": "HuggingFaceH4/zephyr-7b-beta",
@@ -11,10 +12,16 @@ MODELS = {
11
  "Mixtral 8x7B": "mistralai/Mistral-7B-Instruct-v0.3",
12
  "Mixtral Nous-Hermes": "NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO",
13
  "Cohere Command R+": "CohereForAI/c4ai-command-r-plus",
14
- "Cohere Aya-23-35B": "CohereForAI/aya-23-35B"
 
15
  }
16
 
 
 
 
17
  def get_client(model_name):
 
 
18
  model_id = MODELS[model_name]
19
  hf_token = os.getenv("HF_TOKEN")
20
  if not hf_token:
@@ -31,6 +38,12 @@ def respond(
31
  system_message,
32
  ):
33
  try:
 
 
 
 
 
 
34
  client = get_client(model_name)
35
  except ValueError as e:
36
  chat_history.append((message, str(e)))
@@ -77,6 +90,19 @@ def respond(
77
  chat_history.append((message, error_message))
78
  yield chat_history
79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  def clear_conversation():
81
  return []
82
 
@@ -115,4 +141,4 @@ with gr.Blocks() as demo:
115
  clear_button.click(clear_conversation, outputs=chatbot, queue=False)
116
 
117
  if __name__ == "__main__":
118
- demo.launch()
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  import os
4
+ import openai # OpenAI API를 사용하기 위해 추가
5
 
6
  MODELS = {
7
  "Zephyr 7B Beta": "HuggingFaceH4/zephyr-7b-beta",
 
12
  "Mixtral 8x7B": "mistralai/Mistral-7B-Instruct-v0.3",
13
  "Mixtral Nous-Hermes": "NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO",
14
  "Cohere Command R+": "CohereForAI/c4ai-command-r-plus",
15
+ "Cohere Aya-23-35B": "CohereForAI/aya-23-35B",
16
+ "GPT-4o-mini": "openai/gpt-4o-mini" # GPT-4o-mini 모델 추가
17
  }
18
 
19
+ # OpenAI API 키 설정
20
+ openai.api_key = os.getenv("OPENAI_API_KEY")
21
+
22
  def get_client(model_name):
23
+ if model_name == "GPT-4o-mini":
24
+ return None # GPT-4o-mini 모델은 HuggingFace 클라이언트가 필요 없음
25
  model_id = MODELS[model_name]
26
  hf_token = os.getenv("HF_TOKEN")
27
  if not hf_token:
 
38
  system_message,
39
  ):
40
  try:
41
+ if model_name == "GPT-4o-mini":
42
+ assistant_message = call_openai_api(message, system_message, max_tokens, temperature, top_p)
43
+ chat_history.append((message, assistant_message))
44
+ yield chat_history
45
+ return
46
+
47
  client = get_client(model_name)
48
  except ValueError as e:
49
  chat_history.append((message, str(e)))
 
90
  chat_history.append((message, error_message))
91
  yield chat_history
92
 
93
+ def call_openai_api(content, system_message, max_tokens, temperature, top_p):
94
+ response = openai.ChatCompletion.create(
95
+ model="gpt-4o-mini", # GPT-4o-mini 모델 사용
96
+ messages=[
97
+ {"role": "system", "content": system_message},
98
+ {"role": "user", "content": content},
99
+ ],
100
+ max_tokens=max_tokens,
101
+ temperature=temperature,
102
+ top_p=top_p,
103
+ )
104
+ return response.choices[0].message['content']
105
+
106
  def clear_conversation():
107
  return []
108
 
 
141
  clear_button.click(clear_conversation, outputs=chatbot, queue=False)
142
 
143
  if __name__ == "__main__":
144
+ demo.launch()