Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
from langchain_huggingface import ChatHuggingFace, HuggingFacePipeline
|
4 |
+
from transformers import BitsAndBytesConfig
|
5 |
+
import re
|
6 |
+
from deep_translator import (GoogleTranslator,
|
7 |
+
PonsTranslator,
|
8 |
+
LingueeTranslator,
|
9 |
+
MyMemoryTranslator,
|
10 |
+
YandexTranslator,
|
11 |
+
DeeplTranslator,
|
12 |
+
QcriTranslator,
|
13 |
+
single_detection,
|
14 |
+
batch_detection)
|
15 |
+
from pyaspeller import YandexSpeller
|
16 |
+
|
17 |
+
def error_correct_pyspeller(sample_text):
|
18 |
+
""" grammer correction of input text"""
|
19 |
+
speller = YandexSpeller()
|
20 |
+
fixed = speller.spelled(sample_text)
|
21 |
+
return fixed
|
22 |
+
|
23 |
+
def postprocerssing(inp_text: str):
|
24 |
+
"""Post preocessing of the llm response"""
|
25 |
+
inp_text = re.sub('<[^>]+>', '', inp_text)
|
26 |
+
inp_text = inp_text.split('##', 1)[0]
|
27 |
+
inp_text = error_correct_pyspeller(inp_text)
|
28 |
+
return inp_text
|
29 |
+
|
30 |
+
|
31 |
+
quantization_config = BitsAndBytesConfig(
|
32 |
+
load_in_4bit=True,
|
33 |
+
bnb_4bit_quant_type="nf4",
|
34 |
+
bnb_4bit_compute_dtype="float16",
|
35 |
+
bnb_4bit_use_double_quant=True,
|
36 |
+
)
|
37 |
+
|
38 |
+
llm = HuggingFacePipeline.from_model_id(
|
39 |
+
model_id="Danielrahmai1991/nvlm_adapt_basic_model_16bit",
|
40 |
+
task="text-generation",
|
41 |
+
pipeline_kwargs=dict(
|
42 |
+
max_new_tokens=512,
|
43 |
+
do_sample=True,
|
44 |
+
repetition_penalty=1.15,
|
45 |
+
trust_remote_code= True,
|
46 |
+
temperature= 0.70
|
47 |
+
|
48 |
+
),
|
49 |
+
model_kwargs={"quantization_config": quantization_config,
|
50 |
+
},
|
51 |
+
)
|
52 |
+
|
53 |
+
chat_model = ChatHuggingFace(llm=llm)
|
54 |
+
|
55 |
+
# history of the messages
|
56 |
+
messages = []
|
57 |
+
|
58 |
+
def run_llm(prompt):
|
59 |
+
global messages
|
60 |
+
print(f"question is {prompt}")
|
61 |
+
lang = single_detection(prompt, api_key='4ab77f25578d450f0902fb42c66d5e11')
|
62 |
+
if lang == 'en':
|
63 |
+
prompt = error_correct_pyspeller(prompt)
|
64 |
+
en_translated = GoogleTranslator(source='auto', target='en').translate(prompt)
|
65 |
+
messages.append({"role": "user", "content": en_translated})
|
66 |
+
ai_msg = chat_model.invoke(messages, skip_prompt = True)
|
67 |
+
response_of_llm = postprocerssing(ai_msg.content)
|
68 |
+
messages.append({"role": "assistant", "content": response_of_llm})
|
69 |
+
response_of_llm = GoogleTranslator(source='auto', target=lang).translate(response_of_llm)
|
70 |
+
print(f"out is: {response_of_llm}")
|
71 |
+
return response_of_llm
|
72 |
+
|
73 |
+
demo = gr.Interface(fn=run_llm, inputs=["text", ], outputs="text")
|
74 |
+
demo.launch(debug=True, share=True)
|
75 |
+
|
76 |
+
|