AndreasThinks commited on
Commit
4425953
·
verified ·
1 Parent(s): 9f18d40

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -35
app.py CHANGED
@@ -1,15 +1,14 @@
1
  import gradio as gr
2
- import requests
3
  import os
 
4
 
5
- API_URL = os.environ.get("API_URL")
6
 
7
- # Hugging Face API configuration
8
- headers = {"Authorization": f"Bearer {os.environ['HF_TOKEN']}"}
9
-
10
- def query(payload):
11
- response = requests.post(API_URL, headers=headers, json=payload)
12
- return response.json()
13
 
14
  def translate(text, source_lang, target_lang):
15
  if source_lang == target_lang:
@@ -19,50 +18,30 @@ def translate(text, source_lang, target_lang):
19
 
20
  input_text = f"""
21
  ### Instruction: {instruction}
22
-
23
  ### Input: {text}
24
-
25
  ### Response:
26
- """
27
 
28
- output = query({
29
- "inputs": input_text,
30
- "parameters": {
31
- "max_new_tokens": 6000,
32
- "return_text": False,
33
- "return_full_text": False,
34
- "handle_long_generation": "hole"
35
- }
36
- })
37
-
38
- print(output)
39
 
40
- translated_text = output[0]['generated_text']
41
 
42
  return translated_text, input_text, source_lang, target_lang
43
 
44
  def continue_generation(translated_text, input_text, source_lang, target_lang):
45
  full_text = f"{input_text}{translated_text}"
46
 
47
- output = query({
48
- "inputs": full_text,
49
- "parameters": {
50
- "max_new_tokens": 8000,
51
- "return_text": False,
52
- "return_full_text": False,
53
- "handle_long_generation": "hole"
54
- }
55
- })
56
 
57
- new_translated_text = output[0]['generated_text']
58
- updated_translated_text = translated_text + new_translated_text
59
 
60
  return updated_translated_text, input_text, source_lang, target_lang
61
 
62
  # Create the Gradio interface
63
  with gr.Blocks() as iface:
64
  gr.Markdown("# English-Welsh Translator")
65
- gr.Markdown("Translate text between English and Welsh using a Hugging Face Inference Endpoint.")
66
 
67
  with gr.Row():
68
  input_text = gr.Textbox(label="Enter text to translate")
 
1
  import gradio as gr
 
2
  import os
3
+ from llama_cpp import Llama
4
 
5
+ # Load the model
6
 
7
+ llm = Llama.from_pretrained(
8
+ repo_id="AndreasThinks/mistral-7b-english-welsh-translate-GGUF",
9
+ filename="*q4_k_m.gguf",
10
+ verbose=False
11
+ )
 
12
 
13
  def translate(text, source_lang, target_lang):
14
  if source_lang == target_lang:
 
18
 
19
  input_text = f"""
20
  ### Instruction: {instruction}
 
21
  ### Input: {text}
 
22
  ### Response:
23
+ """
24
 
25
+ output = llm(input_text, max_tokens=6000, stop=["### Input:", "### Instruction:"], echo=True)
 
 
 
 
 
 
 
 
 
 
26
 
27
+ translated_text = output['choices'][0]['text'].split("### Response:")[-1].strip()
28
 
29
  return translated_text, input_text, source_lang, target_lang
30
 
31
  def continue_generation(translated_text, input_text, source_lang, target_lang):
32
  full_text = f"{input_text}{translated_text}"
33
 
34
+ output = llm(full_text, max_tokens=8000, stop=["### Input:", "### Instruction:"], echo=True)
 
 
 
 
 
 
 
 
35
 
36
+ new_translated_text = output['choices'][0]['text'].split("### Response:")[-1].strip()
37
+ updated_translated_text = translated_text + " " + new_translated_text
38
 
39
  return updated_translated_text, input_text, source_lang, target_lang
40
 
41
  # Create the Gradio interface
42
  with gr.Blocks() as iface:
43
  gr.Markdown("# English-Welsh Translator")
44
+ gr.Markdown("Translate text between English and Welsh using a local LLM with llama-cpp.")
45
 
46
  with gr.Row():
47
  input_text = gr.Textbox(label="Enter text to translate")