Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from openai import OpenAI
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import MAIAI
|
| 5 |
+
|
| 6 |
+
# Load environment variables
|
| 7 |
+
load_dotenv()
|
| 8 |
+
|
| 9 |
+
def chat(text, history, native_language, language, persona, tone = "Casual", model = "gpt-4o-mini"):
|
| 10 |
+
|
| 11 |
+
print(tone, native_language, language, persona)
|
| 12 |
+
|
| 13 |
+
casual = "This is in a casual, internet texting context, use of local slangs is encouraged." if tone == "Casual" else ""
|
| 14 |
+
|
| 15 |
+
teacher = MAIAI.Agent(model=model, temperature=0.5, role=f"You are a {language} teacher teaching {native_language} speaking student.")
|
| 16 |
+
responder = MAIAI.Agent(model=model, temperature=0.5, role=f"""You are a {language} {persona}. Respond to the user's text in {language} and give {native_language} translation. Refer to Chat History for context. Prompt the user to keep the conversation going. {casual}""")
|
| 17 |
+
|
| 18 |
+
feedback_task = MAIAI.Task(
|
| 19 |
+
agent=teacher,
|
| 20 |
+
goal=f"""Text: {text}
|
| 21 |
+
|
| 22 |
+
Correct any linguistic error in the text and give example driven feedback on how to improve the text in {language}.
|
| 23 |
+
Translate any non-{language} from the text into {language}.
|
| 24 |
+
You MUST give your feedback in {native_language}.
|
| 25 |
+
{casual}
|
| 26 |
+
"""
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
respond_task = MAIAI.Task(
|
| 30 |
+
agent=responder,
|
| 31 |
+
goal=f"""{text}
|
| 32 |
+
|
| 33 |
+
Respond to the text above in {language} and give {native_language} translation.
|
| 34 |
+
Refer to Chat History for context.
|
| 35 |
+
Chat History: {history}"""
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
feedback = feedback_task.execute()
|
| 39 |
+
|
| 40 |
+
yield f"""Feedback:
|
| 41 |
+
{feedback}"""
|
| 42 |
+
|
| 43 |
+
response = respond_task.execute()
|
| 44 |
+
|
| 45 |
+
output = f"""
|
| 46 |
+
***Feedback:***
|
| 47 |
+
|
| 48 |
+
{feedback}
|
| 49 |
+
|
| 50 |
+
-----------
|
| 51 |
+
|
| 52 |
+
***{persona}:***
|
| 53 |
+
|
| 54 |
+
{response}
|
| 55 |
+
"""
|
| 56 |
+
|
| 57 |
+
yield output
|
| 58 |
+
|
| 59 |
+
# Sample Function Call ------------------------------------
|
| 60 |
+
|
| 61 |
+
# feedback,response = chat("Soy jugando Demonslayer! Y tu?", "English","Spanish","friendly lady",,casual_tone="Casual")
|
| 62 |
+
|
| 63 |
+
# print(f"""
|
| 64 |
+
# Feedback: {feedback}
|
| 65 |
+
|
| 66 |
+
# Reply: {response}
|
| 67 |
+
# """)
|
| 68 |
+
|
| 69 |
+
# Gradio ----------------------------------------------------
|
| 70 |
+
chat_interface = gr.ChatInterface(
|
| 71 |
+
fn=chat,
|
| 72 |
+
additional_inputs=[
|
| 73 |
+
gr.components.Dropdown(choices=["English","中文","Spanish"], value="English", label="I speak"),
|
| 74 |
+
gr.components.Dropdown(choices=["English","中文","Spanish"], value="English", label="I want to learn"),
|
| 75 |
+
gr.components.Textbox(value = "LinguAI Chatbot", label="I want to talk to"),
|
| 76 |
+
gr.components.Dropdown(choices=["Casual","Formal"], value="Casual", label="Tone")],
|
| 77 |
+
title="LinguAI"
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
chat_interface.launch()
|