sotosbarl commited on
Commit
caea544
1 Parent(s): 6578e8c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +129 -79
app.py CHANGED
@@ -2,29 +2,38 @@ from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipe
2
  import torch
3
  import pickle
4
  import streamlit as st
 
 
5
  from huggingface_hub import InferenceClient
 
6
 
7
- client = InferenceClient(
8
- "mistralai/Mistral-7B-Instruct-v0.1"
9
- )
10
 
11
 
12
- device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
13
 
14
- from translate import Translator
15
 
16
- def init_session_state():
17
- if 'history' not in st.session_state:
18
- st.session_state.history = ""
19
 
 
 
 
 
20
 
21
- temperature=0.9
22
- max_new_tokens=256
23
- top_p=0.95
24
- repetition_penalty=1.0
 
 
 
25
 
 
 
 
 
 
 
 
26
 
27
- generate_kwargs = dict(
28
  temperature=temperature,
29
  max_new_tokens=max_new_tokens,
30
  top_p=top_p,
@@ -33,89 +42,83 @@ generate_kwargs = dict(
33
  seed=42,
34
  )
35
 
36
- def format_prompt(message, history):
37
- prompt = "<s>"
38
- for user_prompt, bot_response in history:
39
- prompt += f"[INST] {user_prompt} [/INST]"
40
- prompt += f" {bot_response}</s> "
41
- prompt += f"[INST] {message} [/INST]"
42
- return prompt
43
-
44
 
45
- # Initialize session state
46
- init_session_state()
47
 
48
- # pipe = pipeline("text2text-generation", model="google/flan-t5-base")
49
- pipe = pipeline("text-generation", model="microsoft/Phi-3-mini-4k-instruct", trust_remote_code=True)
50
- # pipe = pipeline("text-generation", model="GeneZC/MiniChat-1.5-3B")
51
- # pipe = pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.2")
52
- # model_name = "MoritzLaurer/mDeBERTa-v3-base-mnli-xnli"
53
- # tokenizer = AutoTokenizer.from_pretrained(model_name)
54
- # model = AutoModelForSequenceClassification.from_pretrained(model_name)
55
 
56
- classifier = pipeline("zero-shot-classification", model="MoritzLaurer/mDeBERTa-v3-base-mnli-xnli")
57
 
58
- # with open('chapter_titles.pkl', 'rb') as file:
59
- # titles_astiko = pickle.load(file)
60
- # labels1 = ["κληρονομικό", "ακίνητα", "διαζύγιο"]
61
- # # labels2 = ["αποδοχή κληρονομιάς", "αποποίηση", "διαθήκη"]
62
- # # labels3 = ["μίσθωση", "κυριότητα", "έξωση", "απλήρωτα νοίκια"]
63
 
 
64
 
65
- # titles_astiko = ["γάμος", "αλλοδαπός", "φορολογία", "κληρονομικά", "στέγη", "οικογενειακό", "εμπορικό","κλοπή","απάτη"]
66
- # Load dictionary from the file using pickle
67
  with open('my_dict.pickle', 'rb') as file:
68
  dictionary = pickle.load(file)
69
 
70
- def classify(text,labels):
71
- output = classifier(text, labels, multi_label=False)
72
 
73
- return output
74
-
75
 
76
- text = st.text_input('Enter some text:') # Input field for new text
77
 
78
- if text:
79
-
80
- labels = list(dictionary)
81
 
82
- output = classify(text,labels)
83
 
84
- output = output["labels"][0]
85
 
86
- labels = list(dictionary[output])
87
 
88
- output2 = classify(text,labels)
89
 
90
- output2 = output2["labels"][0]
91
 
92
 
93
- answer = dictionary[output][output2]
94
 
95
- # Create a translator object with specified source and target languages
96
- translator = Translator(from_lang='el', to_lang='en')
97
- translator2 = Translator(from_lang='en', to_lang='el')
98
 
99
- st.text("H ερώτηση σας σχετίζεται με " + output+ " δίκαιο")
100
 
101
 
102
- # Translate the text from Greek to English
103
- answer = translator.translate(answer)
104
- text = translator.translate(text)
105
 
106
- st.text("Πιο συγκεκριμένα σχετίζεται με " + output2)
 
107
 
 
108
 
109
- # text_to_translate2 = text[499:999]
110
- # translated_text2 = translator.translate(text_to_translate2)
 
111
 
112
-
113
 
114
- # st.session_state.history += "Based on this info only:" + answer +" ,answer this question, by reasoning step by step:" + text # Add new text to history
115
- # out = pipe(st.session_state.history, max_new_tokens=256) # Generate output based on history
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
 
117
- history = st.session_state.history
118
- prompt = "Based on this info only:" + answer +" ,answer this question, by reasoning step by step:" + text
119
  formatted_prompt = format_prompt(prompt, history)
120
 
121
  stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
@@ -125,22 +128,69 @@ if text:
125
  output += response.token.text
126
  yield output
127
  return output
 
128
 
129
 
130
- # st.text(st.session_state.history)
131
-
132
- # translated_text2 = translator2.translate(out[0]['generated_text'])
133
- translated_text2 = translator2.translate(output)
134
 
135
- st.text(translated_text2)
136
 
137
- # with st.expander("View Full Output", expanded=False):
138
- # st.write(translated_text2, allow_output_mutation=True)
139
 
140
- # st.text(translated_text2)
141
- # st.text("History: " + st.session_state.history)
142
 
143
- # st.text(output)
144
- # st.text(output2)
145
 
146
- # st.text(answer)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  import torch
3
  import pickle
4
  import streamlit as st
5
+ from translate import Translator
6
+
7
  from huggingface_hub import InferenceClient
8
+ import gradio as gr
9
 
 
 
 
10
 
11
 
 
12
 
 
13
 
 
 
 
14
 
15
+ def classify(text,labels):
16
+ output = classifier(text, labels, multi_label=False)
17
+
18
+ return output
19
 
20
+ def format_prompt(message, history):
21
+ prompt = "<s>"
22
+ for user_prompt, bot_response in history:
23
+ prompt += f"[INST] {user_prompt} [/INST]"
24
+ prompt += f" {bot_response}</s> "
25
+ prompt += f"[INST] {message} [/INST]"
26
+ return prompt
27
 
28
+ def generate(
29
+ prompt, history, temperature=0.7, max_new_tokens=256, top_p=0.95, repetition_penalty=1.1,
30
+ ):
31
+ temperature = float(temperature)
32
+ if temperature < 1e-2:
33
+ temperature = 1e-2
34
+ top_p = float(top_p)
35
 
36
+ generate_kwargs = dict(
37
  temperature=temperature,
38
  max_new_tokens=max_new_tokens,
39
  top_p=top_p,
 
42
  seed=42,
43
  )
44
 
45
+ formatted_prompt = format_prompt(prompt, history)
 
 
 
 
 
 
 
46
 
47
+ stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
48
+ output = ""
49
 
50
+ for response in stream:
51
+ output += response.token.text
52
+ yield output
53
+ return output
 
 
 
54
 
 
55
 
56
+ client = InferenceClient(
57
+ "mistralai/Mistral-7B-Instruct-v0.1"
58
+ )
 
 
59
 
60
+ classifier = pipeline("zero-shot-classification", model="MoritzLaurer/mDeBERTa-v3-base-mnli-xnli")
61
 
 
 
62
  with open('my_dict.pickle', 'rb') as file:
63
  dictionary = pickle.load(file)
64
 
 
 
65
 
66
+ # text = st.text_input('Enter some text:') # Input field for new text
 
67
 
 
68
 
69
+ # labels = list(dictionary)
 
 
70
 
71
+ # output = classify(text,labels)
72
 
73
+ # output = output["labels"][0]
74
 
75
+ # labels = list(dictionary[output])
76
 
77
+ # output2 = classify(text,labels)
78
 
79
+ # output2 = output2["labels"][0]
80
 
81
 
82
+ # answer = dictionary[output][output2]
83
 
84
+ # # Create a translator object with specified source and target languages
85
+ # translator = Translator(from_lang='el', to_lang='en')
86
+ # translator2 = Translator(from_lang='en', to_lang='el')
87
 
88
+ # st.text("H ερώτηση σας σχετίζεται με " + output+ " δίκαιο")
89
 
90
 
91
+ # # Translate the text from Greek to English
92
+ # answer = translator.translate(answer)
93
+ # text = translator.translate(text)
94
 
95
+ # st.text("Πιο συγκεκριμένα σχετίζεται με " + output2)
96
+
97
 
98
+
99
 
100
+ # prompt = "Based on this info only:" + answer +" ,answer this question, by reasoning step by step:" + text
101
+ # formatted_prompt = format_prompt(prompt, history)
102
+ # translated_text2 = translator2.translate(output)
103
 
 
104
 
105
+ def generate(
106
+ prompt, history, temperature=0.7, max_new_tokens=256, top_p=0.95, repetition_penalty=1.1,
107
+ ):
108
+ temperature = float(temperature)
109
+ if temperature < 1e-2:
110
+ temperature = 1e-2
111
+ top_p = float(top_p)
112
+
113
+ generate_kwargs = dict(
114
+ temperature=temperature,
115
+ max_new_tokens=max_new_tokens,
116
+ top_p=top_p,
117
+ repetition_penalty=repetition_penalty,
118
+ do_sample=True,
119
+ seed=42,
120
+ )
121
 
 
 
122
  formatted_prompt = format_prompt(prompt, history)
123
 
124
  stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
 
128
  output += response.token.text
129
  yield output
130
  return output
131
+
132
 
133
 
 
 
 
 
134
 
 
135
 
 
 
136
 
 
 
137
 
 
 
138
 
139
+ additional_inputs=[
140
+ gr.Slider(
141
+ label="Temperature",
142
+ value=0.7,
143
+ minimum=0.0,
144
+ maximum=1.0,
145
+ step=0.05,
146
+ interactive=True,
147
+ info="Higher values produce more diverse outputs",
148
+ ),
149
+ gr.Slider(
150
+ label="Max new tokens",
151
+ value=256,
152
+ minimum=0,
153
+ maximum=1024,
154
+ step=64,
155
+ interactive=True,
156
+ info="The maximum numbers of new tokens",
157
+ ),
158
+ gr.Slider(
159
+ label="Top-p (nucleus sampling)",
160
+ value=0.95,
161
+ minimum=0.0,
162
+ maximum=1,
163
+ step=0.05,
164
+ interactive=True,
165
+ info="Higher values sample more low-probability tokens",
166
+ ),
167
+ gr.Slider(
168
+ label="Repetition penalty",
169
+ value=1.1,
170
+ minimum=1.0,
171
+ maximum=2.0,
172
+ step=0.05,
173
+ interactive=True,
174
+ info="Penalize repeated tokens",
175
+ )
176
+ ]
177
+
178
+ css = """
179
+ #mkd {
180
+ height: 500px;
181
+ overflow: auto;
182
+ border: 1px solid #ccc;
183
+ }
184
+ """
185
+
186
+ with gr.Blocks(css=css) as demo:
187
+ gr.HTML("<h1><center>Mistral 7B Instruct<h1><center>")
188
+ gr.HTML("<h3><center>In this demo, you can chat with <a href='https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.1'>Mistral-7B-Instruct</a> model. 💬<h3><center>")
189
+ gr.HTML("<h3><center>Learn more about the model <a href='https://huggingface.co/docs/transformers/main/model_doc/mistral'>here</a>. 📚<h3><center>")
190
+ gr.ChatInterface(
191
+ generate,
192
+ additional_inputs=additional_inputs,
193
+ examples=[["What is the secret to life?"], ["Write me a recipe for pancakes."]]
194
+ )
195
+
196
+ demo.queue().launch(debug=True)