seawolf2357 commited on
Commit
5ab62a5
ยท
verified ยท
1 Parent(s): fbe3341

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -37
app.py CHANGED
@@ -1,7 +1,9 @@
1
  from huggingface_hub import InferenceClient
2
  import gradio as gr
 
3
 
4
  client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
 
5
 
6
  # ์‹œ์Šคํ…œ ์ธ์ŠคํŠธ๋Ÿญ์…˜์„ ์„ค์ •ํ•˜์ง€๋งŒ ์‚ฌ์šฉ์ž์—๊ฒŒ ๋…ธ์ถœํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
7
  system_instruction = """
@@ -74,39 +76,29 @@ const feedbackLink = `https://promptspellsmith.com/feedback`
74
  """
75
 
76
  def format_prompt(message, history):
77
- prompt = "<s>"
78
- # ์‹œ์Šคํ…œ ์ธ์ŠคํŠธ๋Ÿญ์…˜์„ ํ”„๋กฌํ”„ํŠธ์— ํฌํ•จํ•˜์ง€๋งŒ, ์ด๋ฅผ ์‚ฌ์šฉ์ž์—๊ฒŒ๋Š” ํ‘œ์‹œํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
79
- prompt += f"[SYSTEM] {system_instruction} [/SYSTEM]"
80
  for user_prompt, bot_response in history:
81
- prompt += f"[INST] {user_prompt} [/INST]"
82
- prompt += f" {bot_response}</s> "
83
  prompt += f"[INST] {message} [/INST]"
84
  return prompt
85
 
86
  def generate(prompt, history, temperature=0.1, max_new_tokens=25000, top_p=0.95, repetition_penalty=1.0):
87
- temperature = float(temperature)
88
- if temperature < 1e-2:
89
- temperature = 1e-2
90
- top_p = float(top_p)
91
-
92
- generate_kwargs = dict(
93
- temperature=temperature,
94
- max_new_tokens=max_new_tokens,
95
- top_p=top_p,
96
- repetition_penalty=repetition_penalty,
97
- do_sample=True,
98
- seed=42,
99
- )
100
-
101
  formatted_prompt = format_prompt(prompt, history)
102
-
103
- stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
104
- output = ""
105
-
106
- for response in stream:
107
- output += response.token.text
108
- yield output
109
- return output
 
 
110
 
111
  mychatbot = gr.Chatbot(
112
  avatar_images=["./user.png", "./botm.png"],
@@ -116,23 +108,20 @@ mychatbot = gr.Chatbot(
116
  likeable=True,
117
  )
118
 
119
-
120
  examples = [
121
  ["์ข‹์€ ์˜ˆ์ œ๋ฅผ ์•Œ๋ ค์ค˜."],
122
  ["requirements.txt ์ถœ๋ ฅ"],
123
  ["์ „์ฒด ์ฝ”๋“œ๋ฅผ ๋‹ค์‹œ ์ถœ๋ ฅ"],
124
  ["์ฝ”๋“œ ์˜ค๋ฅ˜๋ฅผ ํ™•์ธํ•˜๊ณ  ์ž์„ธํžˆ ์„ค๋ช…ํ•ด์ค˜."],
125
- ["Huggingface์™€ Gradio๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ๋ฌผ์–ด๋ณด์„ธ์š”."]
126
  ]
127
 
128
-
129
- demo = gr.ChatInterface(
130
  fn=generate,
131
- chatbot=mychatbot,
132
- title="AIQ ์ฝ”๋“œํŒŒ์ผ๋Ÿฟ: OpenLLM v1.12",
133
- retry_btn=None,
134
- undo_btn=None,
135
- examples=examples
136
  )
137
 
138
- demo.queue().launch(show_api=False)
 
1
  from huggingface_hub import InferenceClient
2
  import gradio as gr
3
+ from transformers import GPT2Tokenizer
4
 
5
  client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
6
+ tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
7
 
8
  # ์‹œ์Šคํ…œ ์ธ์ŠคํŠธ๋Ÿญ์…˜์„ ์„ค์ •ํ•˜์ง€๋งŒ ์‚ฌ์šฉ์ž์—๊ฒŒ ๋…ธ์ถœํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
9
  system_instruction = """
 
76
  """
77
 
78
  def format_prompt(message, history):
79
+ prompt = "<s>[SYSTEM] {} [/SYSTEM]".format(system_instruction)
 
 
80
  for user_prompt, bot_response in history:
81
+ prompt += f"[INST] {user_prompt} [/INST]{bot_response}</s> "
 
82
  prompt += f"[INST] {message} [/INST]"
83
  return prompt
84
 
85
  def generate(prompt, history, temperature=0.1, max_new_tokens=25000, top_p=0.95, repetition_penalty=1.0):
86
+ # ํ† ํฐ ์ˆ˜ ๊ณ„์‚ฐ
87
+ input_tokens = len(tokenizer.encode(prompt))
88
+ available_tokens = 32768 - input_tokens # ์ตœ๋Œ€ ํ—ˆ์šฉ ํ† ํฐ ์ˆ˜์—์„œ ์ž…๋ ฅ ํ† ํฐ ์ˆ˜๋ฅผ ๋บ€ ๊ฐ’
89
+ max_new_tokens = min(max_new_tokens, available_tokens) # ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ์ตœ๋Œ€ ํ† ํฐ ์ˆ˜ ์กฐ์ •
90
+
 
 
 
 
 
 
 
 
 
91
  formatted_prompt = format_prompt(prompt, history)
92
+
93
+ try:
94
+ stream = client.text_generation(formatted_prompt, temperature=temperature, max_new_tokens=max_new_tokens,
95
+ top_p=top_p, repetition_penalty=repetition_penalty, do_sample=True, seed=42, stream=True)
96
+ output = ""
97
+ for response in stream:
98
+ output += response.token.text
99
+ yield output
100
+ except Exception as e:
101
+ yield f"Error: {str(e)}"
102
 
103
  mychatbot = gr.Chatbot(
104
  avatar_images=["./user.png", "./botm.png"],
 
108
  likeable=True,
109
  )
110
 
 
111
  examples = [
112
  ["์ข‹์€ ์˜ˆ์ œ๋ฅผ ์•Œ๋ ค์ค˜."],
113
  ["requirements.txt ์ถœ๋ ฅ"],
114
  ["์ „์ฒด ์ฝ”๋“œ๋ฅผ ๋‹ค์‹œ ์ถœ๋ ฅ"],
115
  ["์ฝ”๋“œ ์˜ค๋ฅ˜๋ฅผ ํ™•์ธํ•˜๊ณ  ์ž์„ธํžˆ ์„ค๋ช…ํ•ด์ค˜."],
116
+ ["Huggingface์™€ Gradio๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ๋ฌผ์–ด๋ณด์„ธ์š”."]
117
  ]
118
 
119
+ demo = gr.Interface(
 
120
  fn=generate,
121
+ inputs=gr.Textbox(placeholder="์—ฌ๊ธฐ์— ์งˆ๋ฌธ์„ ์ž…๋ ฅํ•˜์„ธ์š”...", lines=2),
122
+ outputs=gr.Markdown(),
123
+ examples=examples,
124
+ title="AIQ ์ฝ”๋“œํŒŒ์ผ๋Ÿฟ: OpenLLM v1.12"
 
125
  )
126
 
127
+ demo.launch(show_api=False)