Mohannad commited on
Commit
81e4a88
1 Parent(s): c1c886a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -78
app.py CHANGED
@@ -40,94 +40,94 @@ embedding_model = PretrainedSpeakerEmbedding(
40
 
41
 
42
  #LLAMA prep
43
- from huggingface_hub import login
44
 
45
- login("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")
46
 
47
- import torch
48
- import transformers
49
- from transformers import AutoTokenizer, AutoModelForCausalLM
50
- from langchain import HuggingFacePipeline
51
- from langchain import PromptTemplate, LLMChain
52
-
53
-
54
- tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-2-13b-chat-hf",
55
- use_auth_token=True,)
56
-
57
- model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-2-13b-chat-hf",
58
- device_map='auto',
59
- torch_dtype=torch.float16,
60
- use_auth_token=True,
61
- # load_in_8bit=True,
62
- # load_in_4bit=True
63
- )
64
- # Use a pipeline for later
65
- from transformers import pipeline
66
 
67
- pipe = pipeline("text-generation",
68
- model=model,
69
- tokenizer= tokenizer,
70
- torch_dtype=torch.bfloat16,
71
- device_map="auto",
72
- max_new_tokens = 512,
73
- do_sample=True,
74
- top_k=30,
75
- num_return_sequences=1,
76
- eos_token_id=tokenizer.eos_token_id
77
- )
78
 
79
 
80
- import json
81
- import textwrap
82
 
83
- B_INST, E_INST = "[INST]", "[/INST]"
84
- B_SYS, E_SYS = "<<SYS>>\n", "\n<</SYS>>\n\n"
85
- DEFAULT_SYSTEM_PROMPT = """\
86
- You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.
87
 
88
- If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information."""
89
 
90
 
91
 
92
- def get_prompt(instruction, new_system_prompt=DEFAULT_SYSTEM_PROMPT ):
93
- SYSTEM_PROMPT = B_SYS + new_system_prompt + E_SYS
94
- prompt_template = B_INST + SYSTEM_PROMPT + instruction + E_INST
95
- return prompt_template
96
 
97
- def cut_off_text(text, prompt):
98
- cutoff_phrase = prompt
99
- index = text.find(cutoff_phrase)
100
- if index != -1:
101
- return text[:index]
102
- else:
103
- return text
104
 
105
- def remove_substring(string, substring):
106
- return string.replace(substring, "")
107
 
108
 
109
 
110
- def generate(text):
111
- prompt = get_prompt(text)
112
- with torch.autocast('cuda', dtype=torch.bfloat16):
113
- inputs = tokenizer(prompt, return_tensors="pt").to('cuda')
114
- outputs = model.generate(**inputs,
115
- max_new_tokens=512,
116
- eos_token_id=tokenizer.eos_token_id,
117
- pad_token_id=tokenizer.eos_token_id,
118
- )
119
- final_outputs = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
120
- final_outputs = cut_off_text(final_outputs, '</s>')
121
- final_outputs = remove_substring(final_outputs, prompt)
122
 
123
- return final_outputs#, outputs
124
 
125
- def parse_text(text):
126
- wrapped_text = textwrap.fill(text, width=100)
127
- print(wrapped_text +'\n\n')
128
- # return assistant_text
129
 
130
- llm = HuggingFacePipeline(pipeline = pipe, model_kwargs = {'temperature':0})
131
 
132
  def segment_embedding(segment, duration, audio_file):
133
  audio = Audio()
@@ -252,18 +252,19 @@ def answer(context, question):
252
  # inputs = {key: np.array(inputs[key], dtype=np.int64) for key in inputs}
253
  # outputs = onnx_model.run(input_feed=dict(inputs), output_names=None)
254
 
255
- instruction = f"conversation: '''{context}'''"+"\n based on the provided conversation in triple quotes answer next question.\n Question: {text}"
256
 
257
- system_prompt = "You are an expert and answer any question based on conversation. You analys the conversation in light of the question then you answer with yes, no or not clear only. You only output one or two words"
258
 
259
- template = get_prompt(instruction, system_prompt)
260
- print(template)
261
 
262
- prompt = PromptTemplate(template=template, input_variables=["text"])
263
- llm_chain = LLMChain(prompt=prompt, llm=llm)
264
- output = llm_chain.run(question)
265
 
266
- return parse_text(output)
 
267
 
268
 
269
  uploaded_file = st.sidebar.file_uploader("Choose a file")
 
40
 
41
 
42
  #LLAMA prep
43
+ # from huggingface_hub import login
44
 
45
+ # login("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")
46
 
47
+ # import torch
48
+ # import transformers
49
+ # from transformers import AutoTokenizer, AutoModelForCausalLM
50
+ # from langchain import HuggingFacePipeline
51
+ # from langchain import PromptTemplate, LLMChain
52
+
53
+
54
+ # tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-2-13b-chat-hf",
55
+ # use_auth_token=True,)
56
+
57
+ # model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-2-13b-chat-hf",
58
+ # device_map='auto',
59
+ # torch_dtype=torch.float16,
60
+ # use_auth_token=True,
61
+ # # load_in_8bit=True,
62
+ # # load_in_4bit=True
63
+ # )
64
+ # # Use a pipeline for later
65
+ # from transformers import pipeline
66
 
67
+ # pipe = pipeline("text-generation",
68
+ # model=model,
69
+ # tokenizer= tokenizer,
70
+ # torch_dtype=torch.bfloat16,
71
+ # device_map="auto",
72
+ # max_new_tokens = 512,
73
+ # do_sample=True,
74
+ # top_k=30,
75
+ # num_return_sequences=1,
76
+ # eos_token_id=tokenizer.eos_token_id
77
+ # )
78
 
79
 
80
+ # import json
81
+ # import textwrap
82
 
83
+ # B_INST, E_INST = "[INST]", "[/INST]"
84
+ # B_SYS, E_SYS = "<<SYS>>\n", "\n<</SYS>>\n\n"
85
+ # DEFAULT_SYSTEM_PROMPT = """\
86
+ # You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.
87
 
88
+ # If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information."""
89
 
90
 
91
 
92
+ # def get_prompt(instruction, new_system_prompt=DEFAULT_SYSTEM_PROMPT ):
93
+ # SYSTEM_PROMPT = B_SYS + new_system_prompt + E_SYS
94
+ # prompt_template = B_INST + SYSTEM_PROMPT + instruction + E_INST
95
+ # return prompt_template
96
 
97
+ # def cut_off_text(text, prompt):
98
+ # cutoff_phrase = prompt
99
+ # index = text.find(cutoff_phrase)
100
+ # if index != -1:
101
+ # return text[:index]
102
+ # else:
103
+ # return text
104
 
105
+ # def remove_substring(string, substring):
106
+ # return string.replace(substring, "")
107
 
108
 
109
 
110
+ # def generate(text):
111
+ # prompt = get_prompt(text)
112
+ # with torch.autocast('cuda', dtype=torch.bfloat16):
113
+ # inputs = tokenizer(prompt, return_tensors="pt").to('cuda')
114
+ # outputs = model.generate(**inputs,
115
+ # max_new_tokens=512,
116
+ # eos_token_id=tokenizer.eos_token_id,
117
+ # pad_token_id=tokenizer.eos_token_id,
118
+ # )
119
+ # final_outputs = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
120
+ # final_outputs = cut_off_text(final_outputs, '</s>')
121
+ # final_outputs = remove_substring(final_outputs, prompt)
122
 
123
+ # return final_outputs#, outputs
124
 
125
+ # def parse_text(text):
126
+ # wrapped_text = textwrap.fill(text, width=100)
127
+ # print(wrapped_text +'\n\n')
128
+ # # return assistant_text
129
 
130
+ # llm = HuggingFacePipeline(pipeline = pipe, model_kwargs = {'temperature':0})
131
 
132
  def segment_embedding(segment, duration, audio_file):
133
  audio = Audio()
 
252
  # inputs = {key: np.array(inputs[key], dtype=np.int64) for key in inputs}
253
  # outputs = onnx_model.run(input_feed=dict(inputs), output_names=None)
254
 
255
+ # instruction = f"conversation: '''{context}'''"+"\n based on the provided conversation in triple quotes answer next question.\n Question: {text}"
256
 
257
+ # system_prompt = "You are an expert and answer any question based on conversation. You analys the conversation in light of the question then you answer with yes, no or not clear only. You only output one or two words"
258
 
259
+ # template = get_prompt(instruction, system_prompt)
260
+ # print(template)
261
 
262
+ # prompt = PromptTemplate(template=template, input_variables=["text"])
263
+ # llm_chain = LLMChain(prompt=prompt, llm=llm)
264
+ # output = llm_chain.run(question)
265
 
266
+ # return parse_text(output)
267
+ return "please use the other app"
268
 
269
 
270
  uploaded_file = st.sidebar.file_uploader("Choose a file")