sotosbarl commited on
Commit
169dc04
1 Parent(s): 67dbaea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -4
app.py CHANGED
@@ -2,6 +2,13 @@ from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipe
2
  import torch
3
  import pickle
4
  import streamlit as st
 
 
 
 
 
 
 
5
  device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
6
 
7
  from translate import Translator
@@ -10,6 +17,31 @@ def init_session_state():
10
  if 'history' not in st.session_state:
11
  st.session_state.history = ""
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  # Initialize session state
14
  init_session_state()
15
 
@@ -79,14 +111,26 @@ if text:
79
 
80
 
81
 
82
- st.session_state.history += "Based on this info only:" + answer +" ,answer this question, by reasoning step by step:" + text # Add new text to history
83
- out = pipe(st.session_state.history, max_new_tokens=256) # Generate output based on history
 
 
 
 
 
 
 
 
 
 
 
 
84
 
85
 
86
  # st.text(st.session_state.history)
87
 
88
- translated_text2 = translator2.translate(out[0]['generated_text'])
89
-
90
 
91
  st.text(translated_text2)
92
 
 
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
 
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,
31
+ repetition_penalty=repetition_penalty,
32
+ do_sample=True,
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
 
 
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)
122
+ output = ""
123
+
124
+ for response in stream:
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