Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,12 +3,10 @@ from langchain_community.llms import OpenAI
|
|
| 3 |
from langchain.prompts import PromptTemplate
|
| 4 |
import os
|
| 5 |
from dotenv import load_dotenv
|
|
|
|
| 6 |
|
| 7 |
load_dotenv()
|
| 8 |
|
| 9 |
-
open_api_key = os.getenv('OPENAI_API_KEY')
|
| 10 |
-
|
| 11 |
-
os.environ["OPENAI_API_KEY"] = open_api_key
|
| 12 |
|
| 13 |
system_prompt_1 = """
|
| 14 |
You are an advanced AI assistant tasked with helping to transcribes given texts into
|
|
@@ -27,22 +25,36 @@ system_prompt_1 = """
|
|
| 27 |
|
| 28 |
transcribes text: """
|
| 29 |
|
| 30 |
-
|
| 31 |
def translate_text(file, text_input):
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
with open(file.name, 'r', encoding='utf-8') as f:
|
| 35 |
file_text = f.read()
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
| 40 |
|
| 41 |
output_file_path = "translated_file.txt"
|
| 42 |
with open(output_file_path, 'w', encoding='utf-8') as f:
|
| 43 |
f.write(file_translation)
|
| 44 |
|
| 45 |
-
return
|
| 46 |
|
| 47 |
iface = gr.Interface(
|
| 48 |
fn=translate_text,
|
|
@@ -56,7 +68,7 @@ iface = gr.Interface(
|
|
| 56 |
|
| 57 |
],
|
| 58 |
title="Text Transcribes",
|
| 59 |
-
description="Upload a text file and provide a text input to translate the text using LangChain and
|
| 60 |
allow_flagging="never"
|
| 61 |
|
| 62 |
)
|
|
|
|
| 3 |
from langchain.prompts import PromptTemplate
|
| 4 |
import os
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
+
from langchain_huggingface import HuggingFaceEndpoint
|
| 7 |
|
| 8 |
load_dotenv()
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
system_prompt_1 = """
|
| 12 |
You are an advanced AI assistant tasked with helping to transcribes given texts into
|
|
|
|
| 25 |
|
| 26 |
transcribes text: """
|
| 27 |
|
|
|
|
| 28 |
def translate_text(file, text_input):
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
# llm = OpenAI()
|
| 33 |
+
|
| 34 |
+
repo_id = "mistralai/Mistral-7B-Instruct-v0.2"
|
| 35 |
+
|
| 36 |
+
llm = HuggingFaceEndpoint(
|
| 37 |
+
repo_id=repo_id,
|
| 38 |
+
max_length=128,
|
| 39 |
+
temperature=0.5,
|
| 40 |
+
huggingfacehub_api_token=api_token,
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
|
| 44 |
with open(file.name, 'r', encoding='utf-8') as f:
|
| 45 |
file_text = f.read()
|
| 46 |
|
| 47 |
+
prompt = PromptTemplate.from_template(system_prompt_1)
|
| 48 |
+
|
| 49 |
+
llm_chain = prompt | llm
|
| 50 |
+
file_translation = llm_chain.invoke({"text": file_text})
|
| 51 |
+
|
| 52 |
|
| 53 |
output_file_path = "translated_file.txt"
|
| 54 |
with open(output_file_path, 'w', encoding='utf-8') as f:
|
| 55 |
f.write(file_translation)
|
| 56 |
|
| 57 |
+
return file_translation, output_file_path
|
| 58 |
|
| 59 |
iface = gr.Interface(
|
| 60 |
fn=translate_text,
|
|
|
|
| 68 |
|
| 69 |
],
|
| 70 |
title="Text Transcribes",
|
| 71 |
+
description="Upload a text file and provide a text input to translate the text using LangChain and Mistral-7B-Instruct-v0.2 model with predefined system prompts.",
|
| 72 |
allow_flagging="never"
|
| 73 |
|
| 74 |
)
|