Views
No views yet
1import transformers
2import torch
3import json
4
5# Definition of license terms and attitudes
6Terms = {
7 'Place Warranty': 'offer warranty protection (or other support), place warranty on the software licensed',
8 'Add License Terms': 'provide additional license terms',
9 'Add Own Notices': 'add own notices in derivative works',
10 'Ask Distribution Fee': 'ask a fee to distribute a copy',
11 'Combine Libraries': 'place side by side with a library (that is not an application or covered work)',
12 'Copy': 'reproduce the original work in copies',
13 'Distribute': 'distribute original or modified derivative works',
14 'Modify': 'modify the software and create derivatives',
15 'Grant Patents': 'grant rights to use copyrighted patents by the licensor, practice patent claims of contributors to the code',
16 'Publicly Display': 'display the original work publicly',
17 'Publicly Perform': 'perform the original work publicly',
18 'Sublicense': 'incorporate the work into something that has a more restrictive license',
19 'Commercial Use': 'use the software for commercial purposes',
20 'Private Use': 'use or modify the software freely or privately without distributing it',
21 'State Changes': 'state significant changes made to the software, cause modified files to carry prominent notices',
22 'Add Statement For Additional Terms': 'place a statement of the additional terms that apply',
23 'Retain Copyright Notice': 'retain the copyright notice in all copies or substantial uses of the work.',
24 'Include License': 'include the full text of license(license copy) in modified software',
25 'Include Notice': 'notice text needs to be distributed (if it exists) with any derivative work',
26 'Offer Source Code': 'disclose your source code when you distribute the software and make the source for the library available',
27 'Rename': 'the name of the derivative work must differ from original, change software name as to not misrepresent them as the original software',
28 'Retain Disclaimer': 'redistributions of source code must retain disclaimer',
29 'Use TradeMark': 'use contributor’s name, trademark or logo',
30 'Give Credit': 'give explicit credit or acknowledgement to the author with the software',
31 'Include Install Instructions': 'include build & install instructions necessary to modify and reinstall the software',
32 'Liable for Damages': 'the licensor cannot be held liable for any damages arising from the use of the software',
33 'Keep Same License': 'distribute the modified or derived work of the software under the terms and conditions of this license'
34}
35
36
37Attitudes = {"CAN": "Indicates that the licensee can perform the actions, commonly used expressions include: hereby grants to you, you may, you can",
38 "CANNOT": "Indicates that the licensee is not allowed to perform the actions, commonly used expressions include: you may not, you can not, without, prohibit, refuse, disallow, decline, against",
39 "MUST": "Indicates that the licensee must perform the actions, commonly used expressions include: you must, you should, as long as, shall, provided that, ensure that, ask that, have to"}
40
41
42# Create the Prompt
43def create_prompt(term_definition, attitude_definition, license_text):
44 exm = {
45 "Distribute": "CAN",
46 "Use": "CAN",
47 "Modify": "CANNOT"
48 }
49
50 prompt = f"""### OBJECTIVE
51Identify the terms and corresponding attitudes contained in the given license text based on the definition of license terms and attitudes.
52
53### DEFINITION OF TERMS
54{term_definition}
55
56### DEFINITION OF ATTITUDES
57{attitude_definition}
58
59### LICENSE TEXT
60{license_text}
61
62### RESPONSE
63Output the results in the form of JSON key-value pairs, where the key is the term name and the value is the corresponding attitude name.
64
65### Output Format Example
66```
67{json.dumps(exm, indent=2)}
68```
69"""
70 return prompt
71
72
73# Load model and create a pipeline
74model_id = "AnonymousAuthors/License-Llama3-8B"
75
76pipeline = transformers.pipeline(
77 "text-generation",
78 model=model_id,
79 model_kwargs={"torch_dtype": torch.bfloat16},
80 device="auto"
81)
82
83# An example of extracting license terms
84license_text = "you may convey modified covered source (with the effect that you shall also become a licensor) provided that you: a) retain notices as required in subsection 3.2; and b) add a notice to the modified covered source stating that you have modified it, with the date and brief description of how you have modified it."
85
86prompt = create_prompt(Terms, Attitudes, license_text)
87
88terminators = [
89 pipeline.tokenizer.eos_token_id,
90 pipeline.tokenizer.convert_tokens_to_ids("<|eot_id|>")
91]
92
93outputs = pipeline(
94 prompt,
95 max_new_tokens=512,
96 eos_token_id=terminators,
97 pad_token_id=pipeline.tokenizer.eos_token_id,
98 do_sample=True,
99 temperature=0.3,
100 top_p=0.7,
101)
102
103response = outputs[0]["generated_text"][len(prompt):]
104
105print(f"License Text: {license_text}\n")
106print(f"LLM Response: {response}\n")pip install vllm == 0.3.11python -m vllm.entrypoints.openai.api_server \
2 --served-model-name llama3-8b \
3 --model /YOUR_LOCAL_PATH/AnonymousAuthors/License-Llama3-8B \
4 --gpu-memory-utilization 0.9 \
5 --tensor-parallel-size 1 \
6 --host 0.0.0.0 \
7 --port 80001from openai import OpenAI
2
3client = OpenAI(
4 api_key='EMPTY',
5 base_url='http://127.0.0.1:8000/v1',
6)
7
8
9def license_extract(query, model_type='llama3-8b', max_tokens=2048, temperature=0.3, top_p=0.7):
10 resp = client.completions.create(
11 model=model_type,
12 prompt=query,
13 max_tokens=max_tokens,
14 temperature=temperature,
15 top_p=top_p,
16 seed=42)
17
18 response = resp.choices[0].text
19 return response
20
21
22# An example of extracting license terms
23license_text = "you may convey modified covered source (with the effect that you shall also become a licensor) provided that you: a) retain notices as required in subsection 3.2; and b) add a notice to the modified covered source stating that you have modified it, with the date and brief description of how you have modified it."
24
25# For the definition of Terms and Attitudes, please refer to the previous section
26prompt = create_prompt(Terms, Attitudes, license_text)
27
28response = license_extract(prompt, model_type='llama3-8b',
29 max_tokens=1500, temperature=0.3, top_p=0.7)
30
31print(f"License Text: {license_text}\n")
32print(f"LLM Response: {response}\n")