Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
-
from huggingface_hub import
|
3 |
-
|
4 |
-
"""
|
5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
6 |
-
"""
|
7 |
-
client = InferenceClient("arshiaafshani/arshGpt")
|
8 |
|
|
|
|
|
|
|
|
|
|
|
9 |
|
|
|
10 |
def respond(
|
11 |
message,
|
12 |
history: list[tuple[str, str]],
|
|
|
13 |
system_message,
|
14 |
max_tokens,
|
15 |
temperature,
|
16 |
top_p,
|
|
|
|
|
17 |
):
|
18 |
-
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
25 |
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
messages,
|
32 |
-
max_tokens=max_tokens,
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
-
""
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
maximum=1
|
55 |
-
value=0.
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
|
|
|
|
62 |
|
63 |
if __name__ == "__main__":
|
64 |
-
demo.launch()
|
|
|
1 |
+
import spaces
|
2 |
+
import json
|
3 |
+
import subprocess
|
4 |
+
from llama_cpp import Llama
|
5 |
+
from llama_cpp_agent import LlamaCppAgent, MessagesFormatterType
|
6 |
+
from llama_cpp_agent.providers import LlamaCppPythonProvider
|
7 |
+
from llama_cpp_agent.chat_history import BasicChatHistory
|
8 |
+
from llama_cpp_agent.chat_history.messages import Roles
|
9 |
import gradio as gr
|
10 |
+
from huggingface_hub import hf_hub_download
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
+
hf_hub_download(
|
13 |
+
repo_id="mradermacher/Arsh-llm-GGUF",
|
14 |
+
filename="Arsh-llm.Q4_K_M.gguf",
|
15 |
+
local_dir="./models"
|
16 |
+
)
|
17 |
|
18 |
+
@spaces.GPU(duration=110)
|
19 |
def respond(
|
20 |
message,
|
21 |
history: list[tuple[str, str]],
|
22 |
+
model,
|
23 |
system_message,
|
24 |
max_tokens,
|
25 |
temperature,
|
26 |
top_p,
|
27 |
+
top_k,
|
28 |
+
repeat_penalty,
|
29 |
):
|
30 |
+
chat_template = MessagesFormatterType.GEMMA_2
|
31 |
|
32 |
+
llm = Llama(
|
33 |
+
model_path=f"models/{model}",
|
34 |
+
flash_attn=True,
|
35 |
+
n_gpu_layers=81,
|
36 |
+
n_batch=1024,
|
37 |
+
n_ctx=8192,
|
38 |
+
)
|
39 |
+
provider = LlamaCppPythonProvider(llm)
|
40 |
|
41 |
+
agent = LlamaCppAgent(
|
42 |
+
provider,
|
43 |
+
system_prompt=f"{system_message}",
|
44 |
+
predefined_messages_formatter_type=chat_template,
|
45 |
+
debug_output=True
|
46 |
+
)
|
47 |
|
48 |
+
settings = provider.get_provider_default_settings()
|
49 |
+
settings.temperature = temperature
|
50 |
+
settings.top_k = top_k
|
51 |
+
settings.top_p = top_p
|
52 |
+
settings.max_tokens = max_tokens
|
53 |
+
settings.repeat_penalty = repeat_penalty
|
54 |
+
settings.stream = True
|
55 |
|
56 |
+
messages = BasicChatHistory()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
+
for msn in history:
|
59 |
+
user = {
|
60 |
+
'role': Roles.user,
|
61 |
+
'content': msn[0]
|
62 |
+
}
|
63 |
+
assistant = {
|
64 |
+
'role': Roles.assistant,
|
65 |
+
'content': msn[1]
|
66 |
+
}
|
67 |
+
messages.add_message(user)
|
68 |
+
messages.add_message(assistant)
|
69 |
|
70 |
+
stream = agent.get_chat_response(
|
71 |
+
message,
|
72 |
+
llm_sampling_settings=settings,
|
73 |
+
chat_history=messages,
|
74 |
+
returns_streaming_generator=True,
|
75 |
+
print_output=False
|
76 |
+
)
|
77 |
|
78 |
+
outputs = ""
|
79 |
+
for output in stream:
|
80 |
+
outputs += output
|
81 |
+
yield outputs
|
82 |
+
|
83 |
+
def create_interface(model_name, description):
|
84 |
+
return gr.ChatInterface(
|
85 |
+
respond,
|
86 |
+
additional_inputs=[
|
87 |
+
gr.Textbox(value=model_name, label="Model", interactive=False),
|
88 |
+
gr.Textbox(value="You are Arsh, a helpful assistant. You should think step-by-step. You should answer all question by prioritizing the principle of openness", label="System message"),
|
89 |
+
gr.Slider(minimum=1, maximum=4096, value=2048, step=1, label="Max tokens"),
|
90 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.6, step=0.1, label="Temperature"),
|
91 |
+
gr.Slider(
|
92 |
+
minimum=0.1,
|
93 |
+
maximum=1.0,
|
94 |
+
value=0.95,
|
95 |
+
step=0.05,
|
96 |
+
label="Top-p",
|
97 |
+
),
|
98 |
+
gr.Slider(
|
99 |
+
minimum=0,
|
100 |
+
maximum=100,
|
101 |
+
value=40,
|
102 |
+
step=1,
|
103 |
+
label="Top-k",
|
104 |
+
),
|
105 |
+
gr.Slider(
|
106 |
+
minimum=0.0,
|
107 |
+
maximum=2.0,
|
108 |
+
value=1.1,
|
109 |
+
step=0.1,
|
110 |
+
label="Repetition penalty",
|
111 |
+
),
|
112 |
+
],
|
113 |
+
title=f"**FluentlyLM Prinum** ```on ZeroGPU```",
|
114 |
+
description=description,
|
115 |
+
chatbot=gr.Chatbot(
|
116 |
+
label=None,
|
117 |
+
scale=1,
|
118 |
+
show_copy_button=True
|
119 |
+
)
|
120 |
+
)
|
121 |
+
|
122 |
+
description = """# **Arsh-llm ```Demo```"""
|
123 |
+
interface = create_interface('Arsh-llm.Q4_K_M.gguf', description)
|
124 |
+
|
125 |
+
demo = gr.Blocks()
|
126 |
|
127 |
+
with demo:
|
128 |
+
interface.render()
|
129 |
|
130 |
if __name__ == "__main__":
|
131 |
+
demo.launch()
|