Tri4 commited on
Commit
d156568
1 Parent(s): bacba66

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -31
app.py CHANGED
@@ -23,8 +23,6 @@ import requests
23
  import json
24
  import os
25
 
26
-
27
-
28
  # set this key as an environment variable
29
  os.environ["HUGGINGFACEHUB_API_TOKEN"] = st.secrets['huggingface_token']
30
 
@@ -36,29 +34,27 @@ Public_Url = 'https://jikoni-tmodel.hf.space' #endpoint
36
 
37
  def translate(userinput, target_lang, source_lang=None):
38
  if source_lang:
39
- url = f"{Public_Url}/translate_enter/"
40
- data = {
41
- "userinput": userinput,
42
- "source_lang": source_lang,
43
- "target_lang": target_lang,
44
  }
45
- response = requests.post(url, json=data)
46
- result = response.json()
47
- print(type(result))
48
- source_lange = source_lang
49
- translation = result['translated_text']
50
-
51
  else:
52
- url = f"{Public_Url}/translate_detect/"
53
- data = {
54
- "userinput": userinput,
55
- "target_lang": target_lang,
56
- }
57
-
58
- response = requests.post(url, json=data)
59
- result = response.json()
60
- source_lange = result['source_language']
61
- translation = result['translated_text']
62
  return source_lange, translation
63
 
64
  def get_pdf_text(pdf : Union[str, bytes, bytearray]) -> str:
@@ -70,7 +66,6 @@ def get_pdf_text(pdf : Union[str, bytes, bytearray]) -> str:
70
  pdf_text += text
71
  return text
72
 
73
-
74
  def get_text_chunks(text:str) ->list:
75
  text_splitter = CharacterTextSplitter(
76
  separator="\n", chunk_size=1500, chunk_overlap=300, length_function=len
@@ -78,7 +73,6 @@ def get_text_chunks(text:str) ->list:
78
  chunks = text_splitter.split_text(text)
79
  return chunks
80
 
81
-
82
  def get_vectorstore(text_chunks : list) -> FAISS:
83
  model = "sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2"
84
  encode_kwargs = {
@@ -90,13 +84,11 @@ def get_vectorstore(text_chunks : list) -> FAISS:
90
  vectorstore = FAISS.from_texts(texts=text_chunks, embedding=embeddings)
91
  return vectorstore
92
 
93
-
94
  def get_conversation_chain(vectorstore:FAISS) -> ConversationalRetrievalChain:
95
  llm = HuggingFaceHub(
96
- #repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1",
97
  repo_id="TheBloke/Mixtral-8x7B-Instruct-v0.1-GGUF",
98
  task="text-generation",
99
- model_kwargs={"temperature": 0.5, "max_length": 1048},
100
  )
101
 
102
  memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
@@ -105,7 +97,6 @@ def get_conversation_chain(vectorstore:FAISS) -> ConversationalRetrievalChain:
105
  )
106
  return conversation_chain
107
 
108
-
109
  st.markdown ("""
110
  <style> div.stSpinner > div {
111
  text-align:center;
@@ -151,7 +142,6 @@ def main():
151
  st.session_state.conversation = get_conversation_chain(vectorstore)
152
  st.info("done")
153
 
154
- #user_question = st.text_input("chat with your pdf ...")
155
  # show user input
156
  if "messages" not in st.session_state:
157
  st.session_state.messages = []
@@ -179,6 +169,5 @@ def main():
179
  # Signature
180
  st.markdown(footer, unsafe_allow_html=True)
181
 
182
-
183
  if __name__ == '__main__':
184
  main()
 
23
  import json
24
  import os
25
 
 
 
26
  # set this key as an environment variable
27
  os.environ["HUGGINGFACEHUB_API_TOKEN"] = st.secrets['huggingface_token']
28
 
 
34
 
35
  def translate(userinput, target_lang, source_lang=None):
36
  if source_lang:
37
+ url = f"{Public_Url}/translate_enter/"
38
+ data = {
39
+ "userinput": userinput,
40
+ "source_lang": source_lang,
41
+ "target_lang": target_lang,
42
  }
43
+ response = requests.post(url, json=data)
44
+ result = response.json()
45
+ print(type(result))
46
+ source_lange = source_lang
47
+ translation = result['translated_text']
 
48
  else:
49
+ url = f"{Public_Url}/translate_detect/"
50
+ data = {
51
+ "userinput": userinput,
52
+ "target_lang": target_lang,
53
+ }
54
+ response = requests.post(url, json=data)
55
+ result = response.json()
56
+ source_lange = result['source_language']
57
+ translation = result['translated_text']
 
58
  return source_lange, translation
59
 
60
  def get_pdf_text(pdf : Union[str, bytes, bytearray]) -> str:
 
66
  pdf_text += text
67
  return text
68
 
 
69
  def get_text_chunks(text:str) ->list:
70
  text_splitter = CharacterTextSplitter(
71
  separator="\n", chunk_size=1500, chunk_overlap=300, length_function=len
 
73
  chunks = text_splitter.split_text(text)
74
  return chunks
75
 
 
76
  def get_vectorstore(text_chunks : list) -> FAISS:
77
  model = "sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2"
78
  encode_kwargs = {
 
84
  vectorstore = FAISS.from_texts(texts=text_chunks, embedding=embeddings)
85
  return vectorstore
86
 
 
87
  def get_conversation_chain(vectorstore:FAISS) -> ConversationalRetrievalChain:
88
  llm = HuggingFaceHub(
 
89
  repo_id="TheBloke/Mixtral-8x7B-Instruct-v0.1-GGUF",
90
  task="text-generation",
91
+ model_kwargs={"temperature": 0.5, "max_length": 1048}
92
  )
93
 
94
  memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
 
97
  )
98
  return conversation_chain
99
 
 
100
  st.markdown ("""
101
  <style> div.stSpinner > div {
102
  text-align:center;
 
142
  st.session_state.conversation = get_conversation_chain(vectorstore)
143
  st.info("done")
144
 
 
145
  # show user input
146
  if "messages" not in st.session_state:
147
  st.session_state.messages = []
 
169
  # Signature
170
  st.markdown(footer, unsafe_allow_html=True)
171
 
 
172
  if __name__ == '__main__':
173
  main()