Spaces:
Running
Running
seawolf2357
commited on
Commit
β’
83746e4
1
Parent(s):
fc0cd7d
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,84 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|