Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
from langchain.llms import HuggingFaceHub
|
4 |
+
from langchain.prompts import PromptTemplate
|
5 |
+
from langchain.chains import LLMChain
|
6 |
+
|
7 |
+
model_repo = os.getenv('HF_MODEL_REPO')
|
8 |
+
eos_string = "</s>"
|
9 |
+
template = """<s>[INST]<<SYS>>You work as translator. You job is translate user requests from {source} to {target}<<SYS>>
|
10 |
+
{query}[/INST]</s>\n"""
|
11 |
+
|
12 |
+
prompt = PromptTemplate(template=template, input_variables=["source","target","query"])
|
13 |
+
|
14 |
+
model_kwargs={
|
15 |
+
"max_new_tokens":2048,
|
16 |
+
"temperature":0.5,
|
17 |
+
"stop" : ["</s>","<|endoftext|>","<|end|>"]
|
18 |
+
}
|
19 |
+
|
20 |
+
llm = HuggingFaceHub(repo_id=model_repo, task="text-generation", model_kwargs=model_kwargs)
|
21 |
+
chain = LLMChain(prompt=prompt, llm=llm)
|
22 |
+
|
23 |
+
def translation(source, target, text):
|
24 |
+
response=chain.run(question)
|
25 |
+
return response.partition(eos_string)[0]
|
26 |
+
|
27 |
+
inputs = [gr.inputs.Dropdown(lang_codes, default='English', label='Source'),
|
28 |
+
gr.inputs.Dropdown(lang_codes, default='Korean', label='Target'),
|
29 |
+
gr.inputs.Textbox(lines=5, label="Input text"),
|
30 |
+
]
|
31 |
+
|
32 |
+
gr.Interface(fn=translation, inputs, outputs="text").launch()
|