Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,91 +1,70 @@
|
|
1 |
# #refer llama recipes for more info https://github.com/huggingface/huggingface-llama-recipes/blob/main/inference-api.ipynb
|
2 |
# #huggingface-llama-recipes : https://github.com/huggingface/huggingface-llama-recipes/tree/main
|
3 |
-
|
4 |
-
|
5 |
import os
|
6 |
|
7 |
ACCESS_TOKEN = os.getenv("HF_TOKEN")
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
|
30 |
-
|
31 |
|
32 |
-
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
|
44 |
-
|
45 |
-
|
46 |
|
47 |
-
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
# )
|
68 |
-
# if __name__ == "__main__":
|
69 |
-
# service.launch()
|
70 |
-
|
71 |
-
|
72 |
-
from openai import OpenAI
|
73 |
-
|
74 |
-
client = OpenAI(
|
75 |
-
base_url = "https://integrate.api.nvidia.com/v1",
|
76 |
-
api_key = ACCESS_TOKEN
|
77 |
)
|
78 |
-
|
79 |
-
|
80 |
-
model="nvidia/llama-3.1-nemotron-70b-instruct",
|
81 |
-
messages=[{"role":"user","content":"Write a limerick about the wonders of GPU computing."}],
|
82 |
-
temperature=0.5,
|
83 |
-
top_p=1,
|
84 |
-
max_tokens=1024,
|
85 |
-
stream=True
|
86 |
-
)
|
87 |
-
|
88 |
-
for chunk in completion:
|
89 |
-
if chunk.choices[0].delta.content is not None:
|
90 |
-
print(chunk.choices[0].delta.content, end="")
|
91 |
|
|
|
1 |
# #refer llama recipes for more info https://github.com/huggingface/huggingface-llama-recipes/blob/main/inference-api.ipynb
|
2 |
# #huggingface-llama-recipes : https://github.com/huggingface/huggingface-llama-recipes/tree/main
|
3 |
+
import gradio as gr
|
4 |
+
from openai import OpenAI
|
5 |
import os
|
6 |
|
7 |
ACCESS_TOKEN = os.getenv("HF_TOKEN")
|
8 |
|
9 |
+
client = OpenAI(
|
10 |
+
base_url="https://integrate.api.nvidia.com/v1",
|
11 |
+
api_key=ACCESS_TOKEN,
|
12 |
+
)
|
13 |
|
14 |
+
def respond(
|
15 |
+
message,
|
16 |
+
history: list[tuple[str, str]],
|
17 |
+
system_message,
|
18 |
+
max_tokens,
|
19 |
+
temperature,
|
20 |
+
top_p,
|
21 |
+
):
|
22 |
+
messages = [{"role": "system", "content": system_message}]
|
23 |
|
24 |
+
for val in history:
|
25 |
+
if val[0]:
|
26 |
+
messages.append({"role": "user", "content": val[0]})
|
27 |
+
if val[1]:
|
28 |
+
messages.append({"role": "assistant", "content": val[1]})
|
29 |
|
30 |
+
messages.append({"role": "user", "content": message})
|
31 |
|
32 |
+
response = ""
|
33 |
|
34 |
+
for message in client.chat.completions.create(
|
35 |
+
model="nvidia/llama-3.1-nemotron-70b-instruct",
|
36 |
+
max_tokens=max_tokens,
|
37 |
+
stream=True,
|
38 |
+
temperature=temperature,
|
39 |
+
top_p=top_p,
|
40 |
+
messages=messages,
|
41 |
+
):
|
42 |
+
token = message.choices[0].delta.content
|
43 |
|
44 |
+
response += token
|
45 |
+
yield response
|
46 |
|
47 |
+
chatbot = gr.Chatbot(height=600)
|
48 |
|
49 |
+
service = gr.ChatInterface(
|
50 |
+
respond,
|
51 |
+
additional_inputs=[
|
52 |
+
gr.Textbox(value="", label="Системный промпт"),
|
53 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Максимальная длина ответа"),
|
54 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Температура"),
|
55 |
+
gr.Slider(
|
56 |
+
minimum=0.1,
|
57 |
+
maximum=1.0,
|
58 |
+
value=0.95,
|
59 |
+
step=0.05,
|
60 |
+
label="top_p",
|
61 |
+
),
|
62 |
|
63 |
+
],
|
64 |
+
fill_height=True,
|
65 |
+
chatbot=chatbot,
|
66 |
+
theme=gr.themes.Soft(),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
)
|
68 |
+
if __name__ == "__main__":
|
69 |
+
service.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|