Spaces:
Sleeping
Sleeping
import gradio as gr | |
from llama_cpp import Llama | |
# Initialize the model | |
model = Llama( | |
model_path="Fgot_Official_3B.Q4_K_M.gguf", # Replace with your model path | |
n_ctx=4096, # Context window | |
n_threads=2 # Number of CPU threads to use | |
) | |
def format_response(text): | |
# Обрабатываем переносы строк для HTML | |
return text.replace('\n', '<br>') | |
def respond( | |
message, | |
history: list[tuple[str, str]], | |
system_message, | |
max_tokens, | |
temperature, | |
top_p, | |
): | |
# Format the conversation history | |
messages = [{"role": "system", "content": system_message}] | |
for user_msg, assistant_msg in history: | |
if user_msg: | |
messages.append({"role": "user", "content": user_msg}) | |
if assistant_msg: | |
messages.append({"role": "assistant", "content": assistant_msg}) | |
messages.append({"role": "user", "content": message}) | |
# Generate response | |
response = "" | |
stream = model.create_chat_completion( | |
messages=messages, | |
max_tokens=max_tokens, | |
stream=True, | |
temperature=temperature, | |
top_p=top_p | |
) | |
for chunk in stream: | |
# Extract content from the chunk | |
if 'choices' in chunk and len(chunk['choices']) > 0: | |
if 'text' in chunk['choices'][0]: | |
content = chunk['choices'][0]['text'] | |
elif 'delta' in chunk['choices'][0] and 'content' in chunk['choices'][0]['delta']: | |
content = chunk['choices'][0]['delta']['content'] | |
else: | |
continue | |
if content is not None: | |
response += content | |
yield format_response(response) | |
# Create the Gradio interface with HTML formatting | |
demo = gr.ChatInterface( | |
respond, | |
additional_inputs=[ | |
gr.Textbox(value="(Пиши посты)", label="System prompt"), | |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Максимум новых токенов"), | |
gr.Slider(minimum=0.1, maximum=2.0, value=0.42, step=0.01, label="Температура (рандомность)"), | |
gr.Slider( | |
minimum=0.1, | |
maximum=1.0, | |
value=0.95, | |
step=0.05, | |
label="Top-p (работает почти как температура, 0.95 = 95%)", | |
), | |
], | |
) | |
if __name__ == "__main__": | |
demo.launch() |