Danielrahmai1991 commited on
Commit
3a29dde
·
verified ·
1 Parent(s): 42f2ed2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -4
app.py CHANGED
@@ -20,7 +20,30 @@ model, tokenizer = FastLanguageModel.from_pretrained(
20
  FastLanguageModel.for_inference(model)
21
  print("model loaded")
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
 
 
 
 
 
 
 
24
 
25
  # streamer = TextStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens = True)
26
 
@@ -28,7 +51,12 @@ messages = []
28
 
29
  def generate_text(prompt, max_length, top_p, top_k):
30
  global messages
31
- messages.append({"role": "user", "content": prompt})
 
 
 
 
 
32
 
33
  input_ids = tokenizer.apply_chat_template(
34
  messages,
@@ -59,7 +87,8 @@ def generate_text(prompt, max_length, top_p, top_k):
59
  for text in streamer:
60
  generated_text.append(text)
61
  # print(generated_text)
62
- yield "".join(generated_text)
 
63
 
64
  messages.append({"role": "assistant", "content": "".join(generated_text)})
65
 
@@ -67,12 +96,12 @@ description = """
67
  # Deploy our LLM
68
  """
69
  inputs = [
70
- gr.Textbox(label="Prompt text"),
71
  gr.Textbox(label="max-lenth generation", value=100),
72
  gr.Slider(0.0, 1.0, label="top-p value", value=0.95),
73
  gr.Textbox(label="top-k", value=50,),
74
  ]
75
- outputs = [gr.Textbox(label="Generated Text")]
76
 
77
  demo = gr.Interface(fn=generate_text, inputs=inputs, outputs=outputs, description=description)
78
 
 
20
  FastLanguageModel.for_inference(model)
21
  print("model loaded")
22
 
23
+ import re
24
+ from deep_translator import (GoogleTranslator,
25
+ PonsTranslator,
26
+ LingueeTranslator,
27
+ MyMemoryTranslator,
28
+ YandexTranslator,
29
+ DeeplTranslator,
30
+ QcriTranslator,
31
+ single_detection,
32
+ batch_detection)
33
+ from pyaspeller import YandexSpeller
34
+ def error_correct_pyspeller(sample_text):
35
+ """ grammer correction of input text"""
36
+ speller = YandexSpeller()
37
+ fixed = speller.spelled(sample_text)
38
+ return fixed
39
 
40
+ def postprocerssing(inp_text: str):
41
+ """Post preocessing of the llm response"""
42
+ inp_text = re.sub('<[^>]+>', '', inp_text)
43
+ inp_text = inp_text.split('##', 1)[0]
44
+ inp_text = error_correct_pyspeller(inp_text)
45
+ return inp_text
46
+
47
 
48
  # streamer = TextStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens = True)
49
 
 
51
 
52
  def generate_text(prompt, max_length, top_p, top_k):
53
  global messages
54
+ lang = single_detection(prompt, api_key='4ab77f25578d450f0902fb42c66d5e11')
55
+ if lang == 'en':
56
+ prompt = error_correct_pyspeller(prompt)
57
+ en_translated = GoogleTranslator(source='auto', target='en').translate(prompt)
58
+ messages.append({"role": "user", "content": en_translated})
59
+ # messages.append({"role": "user", "content": prompt})
60
 
61
  input_ids = tokenizer.apply_chat_template(
62
  messages,
 
87
  for text in streamer:
88
  generated_text.append(text)
89
  # print(generated_text)
90
+ # yield "".join(generated_text)
91
+ yield GoogleTranslator(source='auto', target=lang).translate("".join(generated_text))
92
 
93
  messages.append({"role": "assistant", "content": "".join(generated_text)})
94
 
 
96
  # Deploy our LLM
97
  """
98
  inputs = [
99
+ gr.Textbox(label="Prompt text", lines=5),
100
  gr.Textbox(label="max-lenth generation", value=100),
101
  gr.Slider(0.0, 1.0, label="top-p value", value=0.95),
102
  gr.Textbox(label="top-k", value=50,),
103
  ]
104
+ outputs = [gr.Textbox(label="Generated Text", lines= 10)]
105
 
106
  demo = gr.Interface(fn=generate_text, inputs=inputs, outputs=outputs, description=description)
107