Spaces:
Runtime error
Runtime error
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,47 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import gradio as gr
|
| 3 |
-
from langchain_core.pydantic_v1 import BaseModel, Field
|
| 4 |
-
from langchain.prompts import HumanMessagePromptTemplate, ChatPromptTemplate
|
| 5 |
-
from langchain.output_parsers import PydanticOutputParser
|
| 6 |
-
from langchain_openai import ChatOpenAI
|
| 7 |
-
|
| 8 |
-
# define the model instance
|
| 9 |
-
chat = ChatOpenAI()
|
| 10 |
-
|
| 11 |
-
class TextTranslator(BaseModel):
|
| 12 |
-
output: str = Field(description="Python string containing the output text translated in the desired language")
|
| 13 |
-
|
| 14 |
-
output_parser = PydanticOutputParser(pydantic_object=TextTranslator)
|
| 15 |
-
format_instructions = output_parser.get_format_instructions()
|
| 16 |
-
|
| 17 |
-
def text_translator(input_text : str, language : str) -> str:
|
| 18 |
-
human_template = """Enter the text that you want to translate:
|
| 19 |
-
{input_text}, and enter the language that you want it to translate to {language}. {format_instructions}"""
|
| 20 |
-
human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)
|
| 21 |
-
|
| 22 |
-
chat_prompt = ChatPromptTemplate.from_messages([human_message_prompt])
|
| 23 |
-
|
| 24 |
-
prompt = chat_prompt.format_prompt(input_text = input_text, language = language, format_instructions = format_instructions)
|
| 25 |
-
|
| 26 |
-
messages = prompt.to_messages()
|
| 27 |
-
|
| 28 |
-
response = chat(messages = messages)
|
| 29 |
-
|
| 30 |
-
output = output_parser.parse(response.content)
|
| 31 |
-
|
| 32 |
-
output_text = output.output
|
| 33 |
-
|
| 34 |
-
return output_text
|
| 35 |
-
|
| 36 |
-
# Interface
|
| 37 |
-
with gr.Blocks() as demo:
|
| 38 |
-
gr.HTML("<h1 align = 'center'> Text Translator </h1>")
|
| 39 |
-
gr.HTML("<h4 align = 'center'> Translate to any language </h4>")
|
| 40 |
-
|
| 41 |
-
inputs = [gr.Textbox(label = "Enter the text that you want to translate"), gr.Textbox(label = "Enter the language that you want it to translate to", placeholder = "Example : Hindi,French,Bengali,etc")]
|
| 42 |
-
generate_btn = gr.Button(value = 'Translate')
|
| 43 |
-
outputs = [gr.Textbox(label = "Translated text")]
|
| 44 |
-
generate_btn.click(fn = text_translator, inputs= inputs, outputs = outputs)
|
| 45 |
-
|
| 46 |
-
if __name__ == '__main__':
|
| 47 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|