AyoubChLin commited on
Commit
1247d89
·
verified ·
1 Parent(s): 11a1e42

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -55
app.py CHANGED
@@ -1,63 +1,65 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
-
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
 
26
- messages.append({"role": "user", "content": message})
 
 
 
 
 
 
 
27
 
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
- """
43
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
44
- """
45
- demo = gr.ChatInterface(
46
- respond,
47
- additional_inputs=[
48
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
49
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
50
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
51
- gr.Slider(
52
- minimum=0.1,
53
- maximum=1.0,
54
- value=0.95,
55
- step=0.05,
56
- label="Top-p (nucleus sampling)",
57
- ),
58
- ],
59
  )
60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
  if __name__ == "__main__":
63
  demo.launch()
 
1
  import gradio as gr
2
+ import torch
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
+ # Initialize model and tokenizer
6
+ model = AutoModelForCausalLM.from_pretrained(
7
+ "microsoft/Phi-3.5-mini-instruct",
8
+ device_map="cuda",
9
+ torch_dtype="auto",
10
+ trust_remote_code=True,
11
+ )
12
+ tokenizer = AutoTokenizer.from_pretrained("microsoft/Phi-3.5-mini-instruct")
13
 
14
+ # Create pipeline
15
+ pipe = pipeline(
16
+ "text-generation",
17
+ model=model,
18
+ tokenizer=tokenizer,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  )
20
 
21
+ # Generation arguments
22
+ generation_args = {
23
+ "max_new_tokens": 1024,
24
+ "return_full_text": False,
25
+ "temperature": 0.0,
26
+ "do_sample": False,
27
+ }
28
+
29
+ def chat(message, history, system_prompt):
30
+ # Prepare messages
31
+ messages = [
32
+ {"role": "system", "content": system_prompt},
33
+ ]
34
+
35
+ # Add history to messages
36
+ for human, assistant in history:
37
+ messages.append({"role": "user", "content": human})
38
+ messages.append({"role": "assistant", "content": assistant})
39
+
40
+ # Add current message
41
+ messages.append({"role": "user", "content": message})
42
+
43
+ # Generate response
44
+ output = pipe(messages, **generation_args)
45
+ response = output[0]['generated_text']
46
+
47
+ return response
48
+
49
+ # Gradio interface
50
+ with gr.Blocks() as demo:
51
+ chatbot = gr.Chatbot()
52
+ msg = gr.Textbox()
53
+ clear = gr.Button("Clear")
54
+ system_prompt = gr.Textbox(label="System Prompt", value="You are a helpful AI assistant.")
55
+
56
+ def respond(message, chat_history):
57
+ bot_message = chat(message, chat_history, system_prompt.value)
58
+ chat_history.append((message, bot_message))
59
+ return "", chat_history
60
+
61
+ msg.submit(respond, [msg, chatbot], [msg, chatbot])
62
+ clear.click(lambda: None, None, chatbot, queue=False)
63
 
64
  if __name__ == "__main__":
65
  demo.launch()