futranbg commited on
Commit
2354351
·
1 Parent(s): 9b71ff4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -16
app.py CHANGED
@@ -31,37 +31,54 @@ model_kwargs={
31
  "max_new_tokens":2048,
32
  "temperature": 0.01,
33
  "truncate": 4096,
34
- "seed" : 256,
35
  "stop" : ["</s>","<|endoftext|>","<|end|>"],
36
  }
37
 
38
  bloom_model_kwargs={
39
  "max_new_tokens":1000,
40
  "temperature": 0.01,
41
- "truncate": 1000,
42
- "seed" : 256,
43
  "stop" : ["</s>","<|endoftext|>","<|end|>","]]]"],
44
  }
45
 
46
  llm1 = HuggingFaceHub(repo_id=llama_repo, task="text-generation", model_kwargs=model_kwargs)
47
  llm2 = HuggingFaceHub(repo_id=starchat_repo, task="text-generation", model_kwargs=model_kwargs)
48
  llm3 = HuggingFaceHub(repo_id=bloom_repo, task="text-generation", model_kwargs=bloom_model_kwargs)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
  def translation(source, target, text):
51
  response = text
52
- try:
53
- input_prompt = bloom_template.replace("{source}", source)
54
- input_prompt = input_prompt.replace("{target}", target)
55
- input_prompt = input_prompt.replace("{query}", text)
56
- response=llm3(input_prompt)
57
- except Exception as e:
58
- print(f"ERROR: LLM show {e}")
59
- input_prompt = starchat_template.replace("{source}", source)
60
- input_prompt = input_prompt.replace("{target}", target)
61
- input_prompt = input_prompt.replace("{query}", text)
62
- response=llm1(input_prompt)
63
- for eot in bloom_model_kwargs['stop']:
64
- response = response.replace(eot,"")
 
65
  return response
66
 
67
  gr.Interface(translation, inputs=["text","text","text"], outputs="text").launch()
 
31
  "max_new_tokens":2048,
32
  "temperature": 0.01,
33
  "truncate": 4096,
34
+ "seed" : 42,
35
  "stop" : ["</s>","<|endoftext|>","<|end|>"],
36
  }
37
 
38
  bloom_model_kwargs={
39
  "max_new_tokens":1000,
40
  "temperature": 0.01,
41
+ "truncate": 1512,
42
+ "seed" : 42,
43
  "stop" : ["</s>","<|endoftext|>","<|end|>","]]]"],
44
  }
45
 
46
  llm1 = HuggingFaceHub(repo_id=llama_repo, task="text-generation", model_kwargs=model_kwargs)
47
  llm2 = HuggingFaceHub(repo_id=starchat_repo, task="text-generation", model_kwargs=model_kwargs)
48
  llm3 = HuggingFaceHub(repo_id=bloom_repo, task="text-generation", model_kwargs=bloom_model_kwargs)
49
+
50
+ def split_text_into_chunks(text, chunk_size=500):
51
+ lines = text.splitlines()
52
+ chunks = []
53
+ temp_chunk = ""
54
+ for line in lines:
55
+ # If adding the current line doesn't exceed the chunk size, add the line to the chunk
56
+ if len(temp_chunk) + len(line) <= chunk_size:
57
+ temp_chunk += line + '\n'
58
+ else:
59
+ # If adding the line exceeds chunk size, store the current chunk and start a new one
60
+ chunks.append(temp_chunk)
61
+ temp_chunk = line + '\n'
62
+ # Don't forget the last chunk
63
+ chunks.append(temp_chunk)
64
+ return chunks
65
 
66
  def translation(source, target, text):
67
  response = text
68
+ chunks = split_text_into_chunks(text)
69
+ for chunk in chunks:
70
+ response = ""
71
+ try:
72
+ input_prompt = bloom_template.replace("{source}", source)
73
+ input_prompt = input_prompt.replace("{target}", target)
74
+ input_prompt = input_prompt.replace("{query}", chunk)
75
+ stchunk = llm3(input_prompt)
76
+ for eot in bloom_model_kwargs['stop']:
77
+ stchunk = stchunk.replace(eot,"\n")
78
+ response += stchunk
79
+ except Exception as e:
80
+ print(f"ERROR: LLM show {e}")
81
+ response = response.strip()
82
  return response
83
 
84
  gr.Interface(translation, inputs=["text","text","text"], outputs="text").launch()