Spaces:
Running
Running
File size: 4,553 Bytes
a7566b2 918d142 a7566b2 918d142 a7566b2 918d142 a7566b2 248afb0 a7566b2 248afb0 a7566b2 248afb0 a7566b2 248afb0 a7566b2 248afb0 a7566b2 248afb0 a7566b2 248afb0 a7566b2 |
1 2 3 4 5 6 7 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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
from functools import partial
import gradio as gr
import httpx
import subprocess
import os
from openai import OpenAI
from cycloud.auth import load_default_credentials
from const import (
LLM_BASE_URL,
AUTH_CMD,
SYSTEM_PROMPTS,
EXAMPLES,
CSS,
HEADER,
FOOTER,
PLACEHOLDER,
ModelInfo,
MODELS,
)
def get_headers(host: str) -> dict:
creds = load_default_credentials()
return {
"Authorization": f"Bearer {creds.access_token}",
"Host": host,
"Accept": "application/json",
"Content-Type": "application/json",
}
def proxy(request: httpx.Request, model_info: ModelInfo) -> httpx.Request:
request.url = request.url.copy_with(path=model_info.endpoint)
request.headers.update(get_headers(host=model_info.host))
return request
def call_llm(
message: str,
history: list[dict],
model_name: str,
system_prompt: str,
max_tokens: int,
temperature: float,
top_p: float,
):
history_openai_format = []
system_prompt_text = SYSTEM_PROMPTS[system_prompt]
if len(history) == 0:
init = {
"role": "system",
"content": system_prompt_text,
}
history_openai_format.append(init)
history_openai_format.append({"role": "user", "content": message})
else:
for human, assistant in history:
history_openai_format.append({"role": "user", "content": human})
history_openai_format.append({"role": "assistant", "content": assistant})
history_openai_format.append({"role": "user", "content": message})
model_info = MODELS[model_name]
client = OpenAI(
api_key="",
base_url=LLM_BASE_URL,
http_client=httpx.Client(
event_hooks={
"request": [partial(proxy, model_info=model_info)],
},
verify=False,
),
)
stream = client.chat.completions.create(
model=f"/data/cyberagent/{model_info.name}",
messages=history_openai_format,
temperature=temperature,
top_p=top_p,
max_tokens=max_tokens,
n=1,
stream=True,
extra_body={"repetition_penalty": 1.1},
)
message = ""
for chunk in stream:
content = chunk.choices[0].delta.content or ""
message = message + content
yield message
def run():
chatbot = gr.Chatbot(
elem_id="chatbot",
scale=1,
show_copy_button=True,
height="70%",
layout="panel",
)
with gr.Blocks(fill_height=True) as demo:
gr.Markdown(HEADER)
gr.ChatInterface(
fn=call_llm,
stop_btn="Stop Generation",
examples=EXAMPLES,
cache_examples=False,
multimodal=False,
chatbot=chatbot,
additional_inputs_accordion=gr.Accordion(
label="Parameters", open=False, render=False
),
additional_inputs=[
gr.Dropdown(
choices=list(MODELS.keys()),
value=list(MODELS.keys())[0],
label="Model",
visible=False,
render=False,
),
gr.Dropdown(
choices=list(SYSTEM_PROMPTS.keys()),
value=list(SYSTEM_PROMPTS.keys())[0],
label="System Prompt",
visible=False,
render=False,
),
gr.Slider(
minimum=1,
maximum=4096,
step=1,
value=1024,
label="Max tokens",
visible=True,
render=False,
),
gr.Slider(
minimum=0,
maximum=1,
step=0.1,
value=0.3,
label="Temperature",
visible=True,
render=False,
),
gr.Slider(
minimum=0,
maximum=1,
step=0.1,
value=1.0,
label="Top-p",
visible=True,
render=False,
),
],
analytics_enabled=False,
)
gr.Markdown(FOOTER)
demo.queue(max_size=256, api_open=False)
demo.launch(share=False, quiet=True)
if __name__ == "__main__":
run()
|