Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from cerebras.cloud.sdk import Cerebras
|
| 4 |
+
|
| 5 |
+
client = Cerebras(
|
| 6 |
+
api_key=os.environ.get("CEREBRAS_API_KEY"),
|
| 7 |
+
)
|
| 8 |
+
|
| 9 |
+
TTILE = """
|
| 10 |
+
<h1 align="center">Try the world's fastest inference 🚀</h1>
|
| 11 |
+
"""
|
| 12 |
+
NOTICE = """
|
| 13 |
+
Current only support Llama3.1 8B and Llama 3.1 70B.
|
| 14 |
+
"""
|
| 15 |
+
|
| 16 |
+
def respond(
|
| 17 |
+
message,
|
| 18 |
+
history: list[tuple[str, str]],
|
| 19 |
+
model_id,
|
| 20 |
+
max_tokens,
|
| 21 |
+
temperature,
|
| 22 |
+
top_p,
|
| 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 |
+
stream = client.chat.completions.create(
|
| 35 |
+
messages=[
|
| 36 |
+
{
|
| 37 |
+
"role": "user",
|
| 38 |
+
"content": "Why is fast inference important?",
|
| 39 |
+
}
|
| 40 |
+
],
|
| 41 |
+
model=model_id,
|
| 42 |
+
max_tokens=max_tokens,
|
| 43 |
+
temperature=temperature,
|
| 44 |
+
top_p=top_p,
|
| 45 |
+
stream=True,
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
for chunk in stream:
|
| 49 |
+
token = chunk.choices[0].delta.content or ""
|
| 50 |
+
response += token
|
| 51 |
+
yield response
|
| 52 |
+
|
| 53 |
+
chatbot = gr.ChatInterface(
|
| 54 |
+
respond,
|
| 55 |
+
additional_inputs=[
|
| 56 |
+
gr.Dropdown(
|
| 57 |
+
["llama3.1-8b", "llama3.1-70b"],
|
| 58 |
+
value="llama3.1-8b",
|
| 59 |
+
label="Models"
|
| 60 |
+
),
|
| 61 |
+
gr.Slider(minimum=1, maximum=8192, value=4096, step=1, label="Max new tokens"),
|
| 62 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 63 |
+
gr.Slider(
|
| 64 |
+
minimum=0.1,
|
| 65 |
+
maximum=1.0,
|
| 66 |
+
value=0.95,
|
| 67 |
+
step=0.05,
|
| 68 |
+
label="Top-p (nucleus sampling)",
|
| 69 |
+
),
|
| 70 |
+
],
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 74 |
+
gr.HTML(TTILE)
|
| 75 |
+
gr.HTML(NOTICE)
|
| 76 |
+
chatbot.render()
|
| 77 |
+
|
| 78 |
+
if __name__ == "__main__":
|
| 79 |
+
demo.launch(debug=True, show_error=True)
|