explorewithai commited on
Commit
9afc14b
·
verified ·
1 Parent(s): 5a9dcb1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -22
app.py CHANGED
@@ -1,47 +1,51 @@
1
  import gradio as gr
2
  import os
3
- from huggingface_hub import InferenceClient
4
 
5
- client = InferenceClient("explorewithai/Loxa-4B")
 
 
6
 
 
7
  meo_system = os.environ.get("MEO")
8
 
9
  def respond(
10
  message,
11
- history: list[tuple[str, str]],
12
  max_tokens,
13
  temperature,
14
  top_p,
15
  ):
 
16
  messages = [{"role": "system", "content": meo_system}]
17
-
18
- for val in history:
19
- if val[0]:
20
- messages.append({"role": "user", "content": val[0]})
21
- if val[1]:
22
- messages.append({"role": "assistant", "content": val[1]})
23
-
24
  messages.append({"role": "user", "content": message})
25
 
26
- response = ""
 
27
 
28
- for message in client.chat_completion(
29
- messages,
30
- max_tokens=max_tokens,
31
- stream=True,
 
32
  temperature=temperature,
33
  top_p=top_p,
34
- ):
35
- token = message.choices[0].delta.content
36
 
37
- response += token
38
- yield response
39
 
 
40
 
 
41
  demo = gr.ChatInterface(
42
  respond,
43
  additional_inputs=[
44
- gr.Slider(minimum=1, maximum=2048, value=2048, step=1, label="Max new tokens"),
45
  gr.Slider(minimum=0.1, maximum=1.0, value=0.7, step=0.1, label="Temperature"),
46
  gr.Slider(
47
  minimum=0.1,
@@ -53,6 +57,5 @@ demo = gr.ChatInterface(
53
  ],
54
  )
55
 
56
-
57
  if __name__ == "__main__":
58
- demo.launch()
 
1
  import gradio as gr
2
  import os
3
+ from transformers import pipeline, AutoTokenizer
4
 
5
+ # Load the tokenizer and model using the pipeline
6
+ pipe = pipeline("text-generation", model="explorewithai/Loxa-4B", trust_remote_code=True)
7
+ tokenizer = AutoTokenizer.from_pretrained("explorewithai/Loxa-4B")
8
 
9
+ # Get the system prompt from environment variables
10
  meo_system = os.environ.get("MEO")
11
 
12
  def respond(
13
  message,
14
+ history,
15
  max_tokens,
16
  temperature,
17
  top_p,
18
  ):
19
+ # Format the messages for the pipeline
20
  messages = [{"role": "system", "content": meo_system}]
21
+ for user_msg, bot_msg in history:
22
+ messages.append({"role": "user", "content": user_msg})
23
+ messages.append({"role": "assistant", "content": bot_msg})
 
 
 
 
24
  messages.append({"role": "user", "content": message})
25
 
26
+ # Generate the prompt using the tokenizer's chat template
27
+ prompt = tokenizer.apply_chat_template(messages, tokenize=False)
28
 
29
+ # Generate the response using the pipeline
30
+ outputs = pipe(
31
+ prompt,
32
+ max_new_tokens=max_tokens,
33
+ do_sample=True,
34
  temperature=temperature,
35
  top_p=top_p,
36
+ return_full_text=False # We only want the generated part
37
+ )
38
 
39
+ # Extract the generated text
40
+ response = outputs[0]['generated_text']
41
 
42
+ return response
43
 
44
+ # Create the Gradio interface
45
  demo = gr.ChatInterface(
46
  respond,
47
  additional_inputs=[
48
+ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
49
  gr.Slider(minimum=0.1, maximum=1.0, value=0.7, step=0.1, label="Temperature"),
50
  gr.Slider(
51
  minimum=0.1,
 
57
  ],
58
  )
59
 
 
60
  if __name__ == "__main__":
61
+ demo.launch()