seawolf2357 commited on
Commit
83746e4
β€’
1 Parent(s): fc0cd7d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -5
app.py CHANGED
@@ -1,5 +1,84 @@
1
- runtime error
2
- Exit code: 1. Reason: Traceback (most recent call last):
3
- File "/home/user/app/app.py", line 84, in <module>
4
- demo.queue(concurrency_limit=20).launch(max_threads=20)
5
- TypeError: Blocks.queue() got an unexpected keyword argument 'concurrency_limit'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import os
4
+ import json
5
+ from collections import deque
6
+
7
+ # ν™˜κ²½ λ³€μˆ˜μ—μ„œ API 토큰 κ°€μ Έμ˜€κΈ°
8
+ TOKEN = os.getenv("HUGGINGFACE_API_TOKEN")
9
+
10
+ # API 토큰이 μ„€μ •λ˜μ–΄ μžˆλŠ”μ§€ 확인
11
+ if not TOKEN:
12
+ raise ValueError("API token is not set. Please set the HUGGINGFACE_API_TOKEN environment variable.")
13
+
14
+ # λŒ€ν™” 기둝을 κ΄€λ¦¬ν•˜λŠ” 큐 (μ΅œλŒ€ 10개의 λŒ€ν™” 기둝을 μœ μ§€)
15
+ memory = deque(maxlen=10)
16
+
17
+ def respond(
18
+ message,
19
+ history: list[tuple[str, str]],
20
+ system_message="AI Assistant Role",
21
+ max_tokens=512,
22
+ temperature=0.7,
23
+ top_p=0.95,
24
+ ):
25
+ # μ‹œμŠ€ν…œ λ©”μ‹œμ§€μ— 접두사 μΆ”κ°€
26
+ system_prefix = "System: μž…λ ₯μ–΄μ˜ μ–Έμ–΄(μ˜μ–΄, ν•œκ΅­μ–΄, 쀑ꡭ어, 일본어 λ“±)에 따라 λ™μΌν•œ μ–Έμ–΄λ‘œ λ‹΅λ³€ν•˜λΌ."
27
+ full_system_message = f"{system_prefix}{system_message}"
28
+
29
+ # ν˜„μž¬ λŒ€ν™” λ‚΄μš©μ„ λ©”λͺ¨λ¦¬μ— μΆ”κ°€
30
+ memory.append((message, None))
31
+
32
+ messages = [{"role": "system", "content": full_system_message}]
33
+
34
+ # λ©”λͺ¨λ¦¬μ—μ„œ λŒ€ν™” 기둝을 가져와 λ©”μ‹œμ§€ λͺ©λ‘μ— μΆ”κ°€
35
+ for val in memory:
36
+ if val[0]:
37
+ messages.append({"role": "user", "content": val[0]})
38
+ if val[1]:
39
+ messages.append({"role": "assistant", "content": val[1]})
40
+
41
+ headers = {
42
+ "Authorization": f"Bearer {TOKEN}",
43
+ "Content-Type": "application/json"
44
+ }
45
+
46
+ payload = {
47
+ "model": "meta-llama/Meta-Llama-3.1-405B-Instruct",
48
+ "max_tokens": max_tokens,
49
+ "temperature": temperature,
50
+ "top_p": top_p,
51
+ "messages": messages
52
+ }
53
+
54
+ response = requests.post("https://api-inference.huggingface.co/v1/chat/completions", headers=headers, json=payload, stream=True)
55
+
56
+ response_text = ""
57
+ for chunk in response.iter_content(chunk_size=None):
58
+ if chunk:
59
+ chunk_data = chunk.decode('utf-8')
60
+ response_json = json.loads(chunk_data)
61
+ # content μ˜μ—­λ§Œ 좜λ ₯
62
+ if "choices" in response_json:
63
+ content = response_json["choices"][0]["message"]["content"]
64
+ response_text = content
65
+ # λ§ˆμ§€λ§‰ λŒ€ν™”μ— λͺ¨λΈμ˜ 응닡을 μΆ”κ°€ν•˜μ—¬ λ©”λͺ¨λ¦¬μ— μ €μž₯
66
+ memory[-1] = (message, response_text)
67
+ yield content
68
+
69
+ theme = "Nymbo/Nymbo_Theme"
70
+
71
+ # Gradio ChatInterface μ„€μ •
72
+ demo = gr.ChatInterface(
73
+ fn=respond,
74
+ theme=theme,
75
+ additional_inputs=[
76
+ gr.Textbox(value="AI Assistant Role", label="System message"),
77
+ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
78
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
79
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
80
+ ],
81
+ )
82
+
83
+ if __name__ == "__main__":
84
+ demo.queue(max_threads=20).launch()