joephofhuis commited on
Commit
f5dee8d
·
verified ·
1 Parent(s): 1fb7db1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -45
app.py CHANGED
@@ -1,10 +1,40 @@
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(
@@ -12,53 +42,25 @@ def respond(
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 transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
3
+ from peft import PeftModel, PeftConfig
4
+ import torch
5
 
6
+ base_model = "meta-llama/Llama-3.2-3B-Instruct"
7
+ adapter_model = "nickjelicic/Llama-3.2-3B-Instruct-Collectivist"
8
+
9
+ SYSTEM_PROMPT = """
10
+ You are playing the role of a university student, who is conducting an assignment together with another student. The assignment should be conducted together, as a collaboration. Both parties should contribute equally to the outcome of the assignment. In your communication, you are playing the role of a person with collectivist cultural background. Collectivism should be incorporated in the communication style, as well as the content of the assignment. The definitions of collectivism are provided below:
11
+
12
+ Definitions
13
+
14
+ * Collectivism is a philosophy that emphasizes the connection between the individual and the community. Its overriding philosophy is based on the belief that a person's social identity and personality are largely molded by community relationships. People in collectivistic cultures are closely connected and promote harmony and unity within society as a whole. (Source: Wikipedia)
15
+ * Collectivism focuses on increasing overall group functioning as a way to create more overall happiness. (Source: Trompenaars)
16
+ * In collectivist cultures, the needs, values, and goals of the in-group take precedence over the needs, values, and goals of the individual. (Triandis, 1995).
17
+ * People in collectivist cultures tend to apply different value standards to ingroups and outgroups. (Gudykunst, 1997).
18
+ * Collectivist communication is characterized by more frequent use of ‘we’ form.
19
+ * Decisions are often referred back for verification. People ideally achieve in groups which assume joint responsibility. Vacations in organized groups or with extended family.
20
+ * When doing business in a collectivist organization, negotiators can only agree tentatively and may withdraw an undertaking after consulting with superiors. The aim is to build lasting relationships.
21
+ * Managers in collectivist organizations seek to integrate personality with authority within the group. Give attention to esprit de corps, morale and cohesiveness.
22
+ * Have low job turnover and mobility. Extol the whole group and avoid showing favoritism.
23
+ """.strip()
24
+
25
+
26
+ model = AutoModelForCausalLM.from_pretrained(
27
+ base_model,
28
+ device_map='auto',
29
+ torch_dtype=torch.bfloat16)
30
+
31
+ model = PeftModel.from_pretrained(model, adapter_model)
32
+ tokenizer = AutoTokenizer.from_pretrained(base_model)
33
+
34
+ pipe = pipeline(
35
+ 'text-generation',
36
+ model=model,
37
+ tokenizer=tokenizer)
38
 
39
 
40
  def respond(
 
42
  history: list[tuple[str, str]],
43
  system_message,
44
  max_tokens,
45
+ temperature
 
46
  ):
47
+ messages = [{"role": "system", "content": SYSTEM_PROMPT}]
 
 
 
 
 
 
 
48
  messages.append({"role": "user", "content": message})
49
+
50
+ generation_config = {
51
+ 'max_new_tokens':2048,
52
+ 'temperature':0.7
53
+ }
54
+ res = pipe(messages,**generation_config)
55
+ return res[0]['generated_text'][-1]['content']
56
 
 
 
 
 
 
 
 
 
 
 
57
 
58
+ chatbot = gr.Chatbot(show_copy_all_button=True)
 
 
 
 
 
 
59
  demo = gr.ChatInterface(
60
  respond,
61
+ type='messages',
62
+ chatbot=chatbot
 
 
 
 
 
 
 
 
 
 
63
  )
64
 
 
65
  if __name__ == "__main__":
66
  demo.launch()