hallisky commited on
Commit
73d39e7
·
1 Parent(s): 3e8b9ba

Description, model inference

Browse files
Files changed (1) hide show
  1. app.py +37 -16
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import spaces
2
  import gradio as gr
3
  import torch
4
- from transformers import AutoTokenizer
5
  import json
6
  from datetime import datetime
7
  from uuid import uuid4
@@ -13,7 +13,6 @@ from huggingface_hub import CommitScheduler
13
  # TODO make it so that feedback is only saved on prev. example if user makes another obfuscation
14
  # and changes slider but doesn't hit obfuscate
15
 
16
- tokenizer = AutoTokenizer.from_pretrained("gpt2")
17
  MODEL_PATHS = {
18
  "length_more": "hallisky/lora-length-long-llama-3-8b",
19
  "length_less": "hallisky/lora-length-long-llama-3-8b",
@@ -23,6 +22,24 @@ MODEL_PATHS = {
23
  "grade_less": "hallisky/lora-grade-elementary-llama-3-8b",
24
  }
25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  # Global variable to store the latest obfuscation result
27
  latest_obfuscation = {}
28
  user_id = str(uuid4()) # Generate a unique session-specific user ID
@@ -40,11 +57,6 @@ scheduler = CommitScheduler(
40
  every=0.5
41
  )
42
 
43
- @spaces.GPU
44
- def temp(text):
45
- response = tokenizer(text, return_tensors="pt")
46
- return response
47
-
48
  def save_data(data):
49
  with scheduler.lock:
50
  with JSON_DATASET_PATH.open("a") as f:
@@ -55,13 +67,12 @@ def save_feedback(feedback_rating, feedback_text):
55
  global latest_obfuscation
56
  if latest_obfuscation:
57
  feedback_data = latest_obfuscation.copy()
58
- feedback_data['feedback'] = {
59
- "rating": feedback_rating,
60
- "text": feedback_text
61
- }
62
  save_data(feedback_data)
63
  return "No Feedback Selected", ""
64
 
 
65
  def greet(input_text, length, function_words, grade_level, sarcasm, formality, voice, persuasive, descriptive, narrative, expository):
66
  global latest_obfuscation, user_id
67
  current_time = datetime.now().isoformat()
@@ -80,7 +91,15 @@ def greet(input_text, length, function_words, grade_level, sarcasm, formality, v
80
  f"Narrative: {narrative}\n"
81
  f"Expository: {expository}"
82
  )
83
-
 
 
 
 
 
 
 
 
84
  # Save the new obfuscation result and reset feedback
85
  latest_obfuscation = {
86
  "datetime": current_time,
@@ -99,10 +118,8 @@ def greet(input_text, length, function_words, grade_level, sarcasm, formality, v
99
  "expository": expository
100
  },
101
  "output": response,
102
- "feedback": {
103
- "rating": "No Feedback Selected",
104
- "text": ""
105
- }
106
  }
107
 
108
  # Save the obfuscation result
@@ -144,6 +161,7 @@ def check_initial_feedback_state(feedback_rating, feedback_text):
144
  demo = gr.Blocks()
145
 
146
  with demo:
 
147
  with gr.Row():
148
  with gr.Column(variant="panel"):
149
  gr.Markdown("# 1) Input Text\n### Enter the text to be obfuscated.")
@@ -206,6 +224,9 @@ with demo:
206
  # Initialize the button and warning message state on page load
207
  demo.load(fn=update_obfuscate_button, inputs=input_text, outputs=[obfuscate_button, warning_message])
208
 
 
 
 
209
  with gr.Column(variant="panel"):
210
  gr.Markdown("# 3) Obfuscated Output")
211
 
 
1
  import spaces
2
  import gradio as gr
3
  import torch
4
+ from transformers import AutoTokenizer, AutoModelForCausalLM
5
  import json
6
  from datetime import datetime
7
  from uuid import uuid4
 
13
  # TODO make it so that feedback is only saved on prev. example if user makes another obfuscation
14
  # and changes slider but doesn't hit obfuscate
15
 
 
16
  MODEL_PATHS = {
17
  "length_more": "hallisky/lora-length-long-llama-3-8b",
18
  "length_less": "hallisky/lora-length-long-llama-3-8b",
 
22
  "grade_less": "hallisky/lora-grade-elementary-llama-3-8b",
23
  }
24
 
25
+ DESCRIPTION = """\
26
+ # Authorship Obfuscation
27
+ This Space demonstrates model [Llama-2-7b-chat](https://huggingface.co/meta-llama/Llama-2-7b-chat) by Meta, a Llama 2 model with 7B parameters fine-tuned for chat instructions. Feel free to play with it, or duplicate to run generations without a queue! If you want to run your own service, you can also [deploy the model on Inference Endpoints](https://huggingface.co/inference-endpoints).
28
+ 🔎 For more details about the Llama 2 family of models and how to use them with `transformers`, take a look [at our blog post](https://huggingface.co/blog/llama2).
29
+ 🔨 Looking for an even more powerful model? Check out the [13B version](https://huggingface.co/spaces/huggingface-projects/llama-2-13b-chat) or the large [70B model demo](https://huggingface.co/spaces/ysharma/Explore_llamav2_with_TGI).
30
+ """
31
+
32
+ # Load models
33
+ if not torch.cuda.is_available():
34
+ device = "cpu"
35
+ DESCRIPTION += "\n<p>Running on CPU 🥶 This demo does not work on CPU.</p>"
36
+
37
+ if torch.cuda.is_available():
38
+ device = "cuda"
39
+ model_id = "meta-llama/Meta-Llama-3-8B"
40
+ model = AutoModelForCausalLM.from_pretrained(model_id).to(device) # device_map="auto" requires accelerate
41
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
42
+
43
  # Global variable to store the latest obfuscation result
44
  latest_obfuscation = {}
45
  user_id = str(uuid4()) # Generate a unique session-specific user ID
 
57
  every=0.5
58
  )
59
 
 
 
 
 
 
60
  def save_data(data):
61
  with scheduler.lock:
62
  with JSON_DATASET_PATH.open("a") as f:
 
67
  global latest_obfuscation
68
  if latest_obfuscation:
69
  feedback_data = latest_obfuscation.copy()
70
+ feedback_data["feedback_rating"] = feedback_rating
71
+ feedback_data["feedback_text"] = feedback_text
 
 
72
  save_data(feedback_data)
73
  return "No Feedback Selected", ""
74
 
75
+ @spaces.GPU
76
  def greet(input_text, length, function_words, grade_level, sarcasm, formality, voice, persuasive, descriptive, narrative, expository):
77
  global latest_obfuscation, user_id
78
  current_time = datetime.now().isoformat()
 
91
  f"Narrative: {narrative}\n"
92
  f"Expository: {expository}"
93
  )
94
+
95
+ with torch.no_grad():
96
+ outputs = model.generate(
97
+ input_ids=tokenizer(input_text, return_tensors="pt").input_ids.to(device),
98
+ max_length=100,
99
+ num_return_sequences=1,
100
+ )
101
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
102
+
103
  # Save the new obfuscation result and reset feedback
104
  latest_obfuscation = {
105
  "datetime": current_time,
 
118
  "expository": expository
119
  },
120
  "output": response,
121
+ "feedback_rating": "No Feedback Selected",
122
+ "feedback_text": ""
 
 
123
  }
124
 
125
  # Save the obfuscation result
 
161
  demo = gr.Blocks()
162
 
163
  with demo:
164
+ gr.Markdown(DESCRIPTION)
165
  with gr.Row():
166
  with gr.Column(variant="panel"):
167
  gr.Markdown("# 1) Input Text\n### Enter the text to be obfuscated.")
 
224
  # Initialize the button and warning message state on page load
225
  demo.load(fn=update_obfuscate_button, inputs=input_text, outputs=[obfuscate_button, warning_message])
226
 
227
+ # with gr.Column(variant="panel"):
228
+ # gr.Markdown("# 3) Obfuscated Output")
229
+
230
  with gr.Column(variant="panel"):
231
  gr.Markdown("# 3) Obfuscated Output")
232