licongwei commited on
Commit
9732969
·
verified ·
1 Parent(s): ae722b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +116 -45
app.py CHANGED
@@ -1,64 +1,135 @@
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
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
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=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.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
 
63
  if __name__ == "__main__":
64
  demo.launch()
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
+ import torch
4
+ from transformers import AutoModelForCausalLM, AutoTokenizer
5
 
6
  """
7
  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
8
  """
9
+ # client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
10
 
11
+ model_id = "GoToCompany/llama3-8b-cpt-sahabatai-v1-instruct"
12
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
13
+ model = AutoModelForCausalLM.from_pretrained(model_id)
14
 
15
+ # def respond(
16
+ # message,
17
+ # history: list[tuple[str, str]],
18
+ # system_message,
19
+ # max_tokens,
20
+ # temperature,
21
+ # top_p,
22
+ # ):
23
+ # messages = [{"role": "system", "content": system_message}]
24
 
25
+ # for val in history:
26
+ # if val[0]:
27
+ # messages.append({"role": "user", "content": val[0]})
28
+ # if val[1]:
29
+ # messages.append({"role": "assistant", "content": val[1]})
30
 
31
+ # messages.append({"role": "user", "content": message})
32
 
33
+ # response = ""
34
 
35
+ # for message in client.chat_completion(
36
+ # messages,
37
+ # max_tokens=max_tokens,
38
+ # stream=True,
39
+ # temperature=temperature,
40
+ # top_p=top_p,
41
+ # ):
42
+ # token = message.choices[0].delta.content
43
 
44
+ # response += token
45
+ # yield response
46
 
47
 
48
+ # """
49
+ # For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
50
+ # """
51
+ # demo = gr.ChatInterface(
52
+ # respond,
53
+ # additional_inputs=[
54
+ # gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
55
+ # gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
56
+ # gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
57
+ # gr.Slider(
58
+ # minimum=0.1,
59
+ # maximum=1.0,
60
+ # value=0.95,
61
+ # step=0.05,
62
+ # label="Top-p (nucleus sampling)",
63
+ # ),
64
+ # ],
65
+ # )
66
+
67
+
68
+ # if __name__ == "__main__":
69
+ # demo.launch()
70
+
71
+
72
+
73
+ # Function to generate text
74
+ def generate_text(prompt, max_length=100):
75
+ inputs = tokenizer(prompt, return_tensors="pt")
76
+ outputs = model.generate(
77
+ **inputs,
78
+ max_length=max_length,
79
+ num_return_sequences=1,
80
+ no_repeat_ngram_size=2,
81
+ do_sample=True,
82
+ top_p=0.95,
83
+ temperature=0.7
84
+ )
85
+ return tokenizer.decode(outputs[0], skip_special_tokens=True)
86
+
87
+ # Gradio frontend
88
+ def gradio_interface(prompt, max_length):
89
+ if not prompt.strip():
90
+ return "Please enter a prompt."
91
+ try:
92
+ output = generate_text(prompt, max_length=max_length)
93
+ return output
94
+ except Exception as e:
95
+ return f"An error occurred: {str(e)}"
96
+
97
+ # Define Gradio components
98
+ with gr.Blocks() as demo:
99
+ gr.Markdown("# LLaMA3 8B CPT Sahabatai Instruct")
100
+ gr.Markdown("Generate text using the **LLaMA3 8B CPT Sahabatai Instruct** model.")
101
+
102
+ with gr.Row():
103
+ with gr.Column():
104
+ prompt_input = gr.Textbox(
105
+ label="Enter your prompt",
106
+ placeholder="Type something...",
107
+ lines=3,
108
+ )
109
+ max_length_slider = gr.Slider(
110
+ label="Max Length",
111
+ minimum=10,
112
+ maximum=200,
113
+ value=100,
114
+ step=10,
115
+ )
116
+ generate_button = gr.Button("Generate")
117
+
118
+ with gr.Column():
119
+ output_text = gr.Textbox(
120
+ label="Generated Text",
121
+ lines=10,
122
+ interactive=False,
123
+ )
124
+
125
+ # Link the button to the function
126
+ generate_button.click(
127
+ fn=gradio_interface,
128
+ inputs=[prompt_input, max_length_slider],
129
+ outputs=output_text,
130
+ )
131
 
132
+ # Launch the Gradio app
133
 
134
  if __name__ == "__main__":
135
  demo.launch()