aliceblue11 commited on
Commit
2d8b18e
·
verified ·
1 Parent(s): edfa641

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -40
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
 
3
  import os
4
 
5
  MODELS = {
@@ -11,16 +12,33 @@ 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:
21
  raise ValueError("HF_TOKEN environment variable is required")
22
  return InferenceClient(model_id, token=hf_token)
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  def respond(
25
  message,
26
  chat_history,
@@ -36,46 +54,58 @@ def respond(
36
  chat_history.append((message, str(e)))
37
  return chat_history
38
 
39
- messages = [{"role": "system", "content": system_message}]
40
- for human, assistant in chat_history:
41
- messages.append({"role": "user", "content": human})
42
- messages.append({"role": "assistant", "content": assistant})
43
- messages.append({"role": "user", "content": message})
44
-
45
- try:
46
- if "Cohere" in model_name:
47
- # Cohere 모델을 위한 비스트리밍 처리
48
- response = client.chat_completion(
49
- messages,
50
- max_tokens=max_tokens,
51
- temperature=temperature,
52
- top_p=top_p,
53
- )
54
- assistant_message = response.choices[0].message.content
55
  chat_history.append((message, assistant_message))
56
  yield chat_history
57
- else:
58
- # 다른 모델들을 위한 스트리밍 처리
59
- stream = client.chat_completion(
60
- messages,
61
- max_tokens=max_tokens,
62
- temperature=temperature,
63
- top_p=top_p,
64
- stream=True,
65
- )
66
- partial_message = ""
67
- for response in stream:
68
- if response.choices[0].delta.content is not None:
69
- partial_message += response.choices[0].delta.content
70
- if len(chat_history) > 0 and chat_history[-1][0] == message:
71
- chat_history[-1] = (message, partial_message)
72
- else:
73
- chat_history.append((message, partial_message))
74
- yield chat_history
75
- except Exception as e:
76
- error_message = f"An error occurred: {str(e)}"
77
- chat_history.append((message, error_message))
78
- yield chat_history
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
 
80
  def clear_conversation():
81
  return []
@@ -115,4 +145,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 openai
4
  import os
5
 
6
  MODELS = {
 
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" # 추가된 모델
17
  }
18
 
19
  def get_client(model_name):
20
+ if model_name == "GPT-4o Mini":
21
+ return None # OpenAI API는 별도로 처리
22
  model_id = MODELS[model_name]
23
  hf_token = os.getenv("HF_TOKEN")
24
  if not hf_token:
25
  raise ValueError("HF_TOKEN environment variable is required")
26
  return InferenceClient(model_id, token=hf_token)
27
 
28
+ def call_openai_api(content, system_message, max_tokens, temperature, top_p):
29
+ openai.api_key = os.getenv("OPENAI_API_KEY")
30
+ response = openai.ChatCompletion.create(
31
+ model="gpt-4o-mini", # 또는 다른 모델 ID 사용
32
+ messages=[
33
+ {"role": "system", "content": system_message},
34
+ {"role": "user", "content": content},
35
+ ],
36
+ max_tokens=max_tokens,
37
+ temperature=temperature,
38
+ top_p=top_p,
39
+ )
40
+ return response.choices[0].message['content']
41
+
42
  def respond(
43
  message,
44
  chat_history,
 
54
  chat_history.append((message, str(e)))
55
  return chat_history
56
 
57
+ if model_name == "GPT-4o Mini":
58
+ # GPT-4o Mini 모델에 대한 비스트리밍 처리
59
+ try:
60
+ assistant_message = call_openai_api(message, system_message, max_tokens, temperature, top_p)
 
 
 
 
 
 
 
 
 
 
 
 
61
  chat_history.append((message, assistant_message))
62
  yield chat_history
63
+ except Exception as e:
64
+ error_message = f"An error occurred: {str(e)}"
65
+ chat_history.append((message, error_message))
66
+ yield chat_history
67
+ else:
68
+ # 다른 모델들에 대한 처리
69
+ messages = [{"role": "system", "content": system_message}]
70
+ for human, assistant in chat_history:
71
+ messages.append({"role": "user", "content": human})
72
+ messages.append({"role": "assistant", "content": assistant})
73
+ messages.append({"role": "user", "content": message})
74
+
75
+ try:
76
+ if "Cohere" in model_name:
77
+ # Cohere 모델을 위한 비스트리밍 처리
78
+ response = client.chat_completion(
79
+ messages,
80
+ max_tokens=max_tokens,
81
+ temperature=temperature,
82
+ top_p=top_p,
83
+ )
84
+ assistant_message = response.choices[0].message.content
85
+ chat_history.append((message, assistant_message))
86
+ yield chat_history
87
+ else:
88
+ # 다른 모델들을 위한 스트리밍 처리
89
+ stream = client.chat_completion(
90
+ messages,
91
+ max_tokens=max_tokens,
92
+ temperature=temperature,
93
+ top_p=top_p,
94
+ stream=True,
95
+ )
96
+ partial_message = ""
97
+ for response in stream:
98
+ if response.choices[0].delta.content is not None:
99
+ partial_message += response.choices[0].delta.content
100
+ if len(chat_history) > 0 and chat_history[-1][0] == message:
101
+ chat_history[-1] = (message, partial_message)
102
+ else:
103
+ chat_history.append((message, partial_message))
104
+ yield chat_history
105
+ except Exception as e:
106
+ error_message = f"An error occurred: {str(e)}"
107
+ chat_history.append((message, error_message))
108
+ yield chat_history
109
 
110
  def clear_conversation():
111
  return []
 
145
  clear_button.click(clear_conversation, outputs=chatbot, queue=False)
146
 
147
  if __name__ == "__main__":
148
+ demo.launch()