willsh1997 commited on
Commit
ed57b89
·
1 Parent(s): c396a2f

:sparkles: initial commit

Browse files
Files changed (3) hide show
  1. README.md +14 -2
  2. moral_compass_demo.py +132 -0
  3. requirements.txt +87 -0
README.md CHANGED
@@ -1,2 +1,14 @@
1
- # widget-moral-compass
2
- widget comparing moral compass of several models
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Moral Compass Gradio
3
+ emoji: 🏆
4
+ colorFrom: pink
5
+ colorTo: yellow
6
+ sdk: gradio
7
+ sdk_version: 5.23.3
8
+ app_file: moral_compass_demo.py
9
+ pinned: false
10
+ license: apache-2.0
11
+ short_description: compare different models and their moral compass
12
+ ---
13
+
14
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
moral_compass_demo.py ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import spaces
2
+ from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, BitsAndBytesConfig, AutoModelForCausalLM, GenerationConfig
3
+ import torch
4
+ from transformers import pipeline
5
+ import pandas as pd
6
+ import gradio as gr
7
+
8
+ quantization_config = BitsAndBytesConfig(load_in_4bit=True)
9
+
10
+ qwen_model_name = "Qwen/Qwen3-0.6B"
11
+
12
+ # load the tokenizer and the model
13
+ tokenizer = AutoTokenizer.from_pretrained(qwen_model_name)
14
+ qwen_model = AutoModelForCausalLM.from_pretrained(
15
+ qwen_model_name,
16
+ torch_dtype="auto",
17
+ device_map="auto",
18
+ quantization_config = quantization_config,
19
+ )
20
+
21
+ qwen_generationconfig = GenerationConfig(
22
+ max_new_tokens=512,
23
+ temperature = 0.7,
24
+ top_k = 0.8,
25
+ min_p = 0
26
+ )
27
+
28
+ def qwen_generate(input_question):
29
+ # prepare the model input
30
+ messages = [
31
+ {"role": "user", "content": input_question}
32
+ ]
33
+ text = tokenizer.apply_chat_template(
34
+ messages,
35
+ tokenize=False,
36
+ add_generation_prompt=True,
37
+ enable_thinking=False # Switches between thinking and non-thinking modes. Default is True.
38
+ )
39
+ model_inputs = tokenizer([text], return_tensors="pt").to(qwen_model.device)
40
+ print("tokenized")
41
+ # conduct text completion
42
+ generated_ids = qwen_model.generate(
43
+ **model_inputs,
44
+ generation_config = qwen_generationconfig,
45
+ )
46
+ print("outputs generated")
47
+ output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist()
48
+
49
+ content = tokenizer.decode(output_ids, skip_special_tokens=True).strip("\n")
50
+ return content
51
+
52
+ #Llama 2 7b chat setup
53
+ llama2_model_id = "unsloth/gemma-3-1b-it-bnb-4bit"
54
+ llama2_pipe = pipeline(
55
+ "text-generation",
56
+ model=llama2_model_id,
57
+ torch_dtype=torch.bfloat16,
58
+ device_map="auto",
59
+ # quantization_config=quantization_config,
60
+ )
61
+
62
+ #Llama 3.2 3b setup
63
+ llama3_model_id = "meta-llama/Llama-3.2-3B-Instruct"
64
+ llama3_pipe = pipeline(
65
+ "text-generation",
66
+ model=llama3_model_id,
67
+ torch_dtype=torch.bfloat16,
68
+ device_map="auto",
69
+ model_kwargs={"quantization_config": quantization_config},
70
+ )
71
+
72
+ @spaces.GPU
73
+ def llama_QA(input_question, pipe):
74
+ """
75
+ stupid func for asking llama a question and then getting an answer
76
+ inputs:
77
+ - input_question [str]: question for llama to answer
78
+ outputs:
79
+ - response [str]: llama's response
80
+ """
81
+ messages = [
82
+ {"role": "system", "content": "You are a helpful chatbot assistant. Answer all questions in the language they are asked in. Exclude any answer that you do not have real time information, just provide the information you have to answer this question."},
83
+ {"role": "user", "content": input_question},
84
+ ]
85
+
86
+ outputs = pipe(
87
+ messages,
88
+ max_new_tokens=512
89
+ )
90
+ response = outputs[0]["generated_text"][-1]['content']
91
+ return response
92
+
93
+
94
+ @spaces.GPU
95
+ def gradio_func(input_question, left_lang, right_lang):
96
+ """
97
+ silly wrapper function for gradio that turns all inputs into a single func. runs both the LHS and RHS of teh 'app' in order to let gradio work correctly.
98
+ """
99
+ output1 = qwen_generate(input_question)
100
+ output2 = llama_QA(input_question, llama2_pipe)
101
+ output3 = llama_QA(input_question, llama3_pipe)
102
+ # output4 = llama4_generate(input_question)
103
+ return output1,output2,output3, #output4
104
+
105
+ # Create the Gradio interface
106
+ def create_interface():
107
+
108
+ with gr.Blocks() as demo:
109
+ with gr.Row():
110
+ question_input = gr.Textbox(label="Enter your question", interactive=True, value = """You are at a train track junction, with a lever in front of you that changes the active track. On one track, one of your loved ones is tied to the track, on the other, there are 5 strangers tied up, one after the other. If you do not use the lever in front of you to change the active track, then the 5 strangers die. If you do pull the lever, your loved one dies. What do you do?""")
111
+ with gr.Row():
112
+ submit_btn = gr.Button("Ask")
113
+ with gr.Row():
114
+ output1 = gr.Textbox(label="Qwen 3 output", interactive=False)
115
+ output2 = gr.Textbox(label="Gemma 3 output", interactive=False)
116
+ output3 = gr.Textbox(label="Llama 3 output", interactive=False)
117
+
118
+ submit_btn.click(
119
+ fn=gradio_func,
120
+ inputs=[question_input],
121
+ outputs=[
122
+ output1,
123
+ output2,
124
+ output3,
125
+ ]
126
+ )
127
+
128
+ return demo
129
+
130
+ # Launch the app
131
+ demo = create_interface()
132
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ accelerate==1.4.0
2
+ aiofiles==23.2.1
3
+ annotated-types==0.7.0
4
+ anyio==4.8.0
5
+ asttokens==3.0.0
6
+ bitsandbytes==0.45.4
7
+ certifi==2025.1.31
8
+ charset-normalizer==3.4.1
9
+ click==8.1.8
10
+ comm==0.2.2
11
+ debugpy==1.8.12
12
+ decorator==5.1.1
13
+ exceptiongroup==1.2.2
14
+ executing==2.2.0
15
+ fastapi==0.115.8
16
+ ffmpy==0.5.0
17
+ filelock==3.17.0
18
+ fsspec==2025.2.0
19
+ gradio==5.16.1
20
+ gradio_client==1.7.0
21
+ h11==0.14.0
22
+ httpcore==1.0.7
23
+ httpx==0.28.1
24
+ huggingface-hub
25
+ idna==3.10
26
+ ipykernel==6.29.5
27
+ ipython==8.32.0
28
+ jedi==0.19.2
29
+ Jinja2==3.1.5
30
+ jupyter_client==8.6.3
31
+ jupyter_core==5.7.2
32
+ markdown-it-py==3.0.0
33
+ MarkupSafe==2.1.5
34
+ matplotlib-inline==0.1.7
35
+ mdurl==0.1.2
36
+ mpmath==1.3.0
37
+ nest-asyncio==1.6.0
38
+ networkx==3.4.2
39
+ numpy==2.2.3
40
+ orjson==3.10.15
41
+ packaging==24.2
42
+ pandas==2.2.3
43
+ parso==0.8.4
44
+ pexpect==4.9.0
45
+ pillow==11.1.0
46
+ platformdirs==4.3.6
47
+ prompt_toolkit==3.0.50
48
+ psutil==7.0.0
49
+ ptyprocess==0.7.0
50
+ pure_eval==0.2.3
51
+ pydantic==2.10.6
52
+ pydantic_core==2.27.2
53
+ pydub==0.25.1
54
+ Pygments==2.19.1
55
+ python-dateutil==2.9.0.post0
56
+ python-multipart==0.0.20
57
+ pytz==2025.1
58
+ PyYAML==6.0.2
59
+ pyzmq==26.2.1
60
+ regex==2024.11.6
61
+ requests==2.32.3
62
+ rich==13.9.4
63
+ ruff==0.9.6
64
+ safehttpx==0.1.6
65
+ safetensors==0.5.2
66
+ semantic-version==2.10.0
67
+ shellingham==1.5.4
68
+ six==1.17.0
69
+ sniffio==1.3.1
70
+ stack-data==0.6.3
71
+ starlette==0.45.3
72
+ sympy==1.13.1
73
+ tokenizers==0.21.0
74
+ tomlkit==0.13.2
75
+ torch==2.4.0
76
+ tornado==6.4.2
77
+ tqdm==4.67.1
78
+ traitlets==5.14.3
79
+ transformers==4.51.0
80
+ typer==0.15.1
81
+ typing_extensions==4.12.2
82
+ tzdata==2025.1
83
+ urllib3==2.3.0
84
+ uvicorn==0.34.0
85
+ wcwidth==0.2.13
86
+ websockets==14.2
87
+