budikomarudin commited on
Commit
5764bd4
·
verified ·
1 Parent(s): a937de5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -33
app.py CHANGED
@@ -3,48 +3,61 @@ import os
3
  from langchain_openai import ChatOpenAI
4
 
5
  api_key = os.environ.get("FEATHERLESS_API_KEY")
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
- MODEL_CHOICES = [
8
- "Qwen/Qwen3-32B",
9
- "deepseek-ai/DeepSeek-R1-Distill-Llama-70B",
10
- "meta-llama/Llama-3.3-70B-Instruct",
11
- "mistralai/Magistral-Small-2506",
12
- "unsloth/DeepSeek-R1-Distill-Llama-70B",
13
- "unsloth/Qwen2.5-72B-Instruct",
14
- "unsloth/Llama-3.3-70B-Instruct",
15
- ]
16
 
17
- def create_llm(model_name: str):
18
- return ChatOpenAI(
19
- base_url="https://api.featherless.ai/v1/",
20
- api_key=api_key,
21
- streaming=True,
22
- model=model_name,
23
- )
24
 
25
- def respond(message, history, system_message, max_tokens, temperature, top_p, model_name):
26
- llm = create_llm(model_name)
27
- messages = [{"role":"system","content":system_message}]
28
- for u, a in history:
29
- if u: messages.append({"role":"user","content":u})
30
- if a: messages.append({"role":"assistant","content":a})
31
- messages.append({"role":"user","content":message})
32
  llm.max_tokens = max_tokens
33
  llm.temperature = temperature
34
  llm.top_p = top_p
35
  response = ""
 
36
  for chunk in llm.stream(messages):
37
- response += chunk.content
 
38
  yield response
39
 
40
- with gr.Blocks() as demo:
41
- with gr.Row():
42
- model_dropdown = gr.Dropdown(choices=MODEL_CHOICES, value=MODEL_CHOICES[0], label="Pilih model")
43
- chatbot = gr.Chatbot()
44
- system_msg = gr.Textbox("You are a friendly Chatbot.", label="System message")
45
- max_t = gr.Slider(1, 16384, value=2048, step=1, label="Max new tokens")
46
- temp = gr.Slider(0.1, 2.0, value=0.7, step=0.1, label="Temperature")
47
- top_p = gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top‑p")
 
 
 
 
 
 
 
48
 
49
- model_dropdown.change(lambda x: None, inputs=model_dropdown, outputs=[])
50
  demo.launch()
 
3
  from langchain_openai import ChatOpenAI
4
 
5
  api_key = os.environ.get("FEATHERLESS_API_KEY")
6
+ llm = ChatOpenAI(
7
+ base_url="https://api.featherless.ai/v1/",
8
+ api_key=api_key,
9
+ streaming=True,
10
+ model="Qwen/Qwen3-32B",
11
+ # model="deepseek-ai/DeepSeek-R1-Distill-Llama-70B",
12
+ # model="meta-llama/Llama-3.3-70B-Instruct",
13
+ # model="mistralai/Magistral-Small-2506",
14
+ # model="unsloth/DeepSeek-R1-Distill-Llama-70B",
15
+ # model="unsloth/Qwen2.5-72B-Instruct",
16
+ # model="unsloth/Llama-3.3-70B-Instruct",
17
+ )
18
 
19
+ def respond(
20
+ message,
21
+ history: list[tuple[str, str]],
22
+ system_message,
23
+ max_tokens,
24
+ temperature,
25
+ top_p,
26
+ ):
27
+ messages = [{"role": "system", "content": system_message}]
28
 
29
+ for val in history:
30
+ if val[0]:
31
+ messages.append({"role": "user", "content": val[0]})
32
+ if val[1]:
33
+ messages.append({"role": "assistant", "content": val[1]})
 
 
34
 
35
+ messages.append({"role": "user", "content": message})
 
 
 
 
 
 
36
  llm.max_tokens = max_tokens
37
  llm.temperature = temperature
38
  llm.top_p = top_p
39
  response = ""
40
+
41
  for chunk in llm.stream(messages):
42
+ token = chunk.content
43
+ response += token
44
  yield response
45
 
46
+ demo = gr.ChatInterface(
47
+ respond,
48
+ additional_inputs=[
49
+ gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
+ gr.Slider(minimum=1, maximum=16384, value=2048, step=1, label="Max new tokens"),
51
+ gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature"),
52
+ gr.Slider(
53
+ minimum=0.1,
54
+ maximum=1.0,
55
+ value=0.95,
56
+ step=0.05,
57
+ label="Top-p (nucleus sampling)",
58
+ ),
59
+ ],
60
+ )
61
 
62
+ if __name__ == "__main__":
63
  demo.launch()