Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,37 @@
|
|
1 |
-
# Path: chatbot_model_based_autocorrect.py
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import os, getpass
|
3 |
from langchain_openai import ChatOpenAI
|
4 |
from langchain_core.messages import HumanMessage
|
@@ -6,28 +39,50 @@ import gradio as gr
|
|
6 |
import openai
|
7 |
|
8 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
|
|
9 |
# Define the language model
|
10 |
model = ChatOpenAI(model="gpt-4o-mini")
|
11 |
|
12 |
# Function to generate a conversational response with model-based autocorrection
|
13 |
def chatbot_autocorrect_response(input_text: str):
|
14 |
-
#
|
15 |
prompt = (
|
16 |
-
f"The user said: '{input_text}'. Please correct this sentence if necessary,
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
20 |
human_message = HumanMessage(content=prompt)
|
21 |
response = model.invoke([human_message])
|
|
|
22 |
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
return response.content
|
25 |
|
26 |
-
#
|
27 |
-
def gradio_chatbot(input_text):
|
28 |
-
#
|
29 |
-
|
|
|
|
|
30 |
|
31 |
-
# Launch Gradio interface
|
32 |
-
interface = gr.Interface(
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# # Path: chatbot_model_based_autocorrect.py
|
2 |
+
# import os, getpass
|
3 |
+
# from langchain_openai import ChatOpenAI
|
4 |
+
# from langchain_core.messages import HumanMessage
|
5 |
+
# import gradio as gr
|
6 |
+
# import openai
|
7 |
+
|
8 |
+
# openai.api_key = os.getenv("OPENAI_API_KEY")
|
9 |
+
# # Define the language model
|
10 |
+
# model = ChatOpenAI(model="gpt-4o-mini")
|
11 |
+
|
12 |
+
# # Function to generate a conversational response with model-based autocorrection
|
13 |
+
# def chatbot_autocorrect_response(input_text: str):
|
14 |
+
# # Step 1: Define the prompt asking the model to correct the sentence
|
15 |
+
# prompt = (
|
16 |
+
# f"The user said: '{input_text}'. Please correct this sentence if necessary, and make it sounds friendly, casual tone, acknowledging the correction and make it sounds like american native conversation. If appropriate, make it sounds like an IELTS 9.0 level response. If sentences are in Indonesian translate it and make it sounds like native american conversational. Please only respond with the corrected sentence. If nothing needs to be changed, repeat the sentence."
|
17 |
+
# )
|
18 |
+
|
19 |
+
# # Step 2: Send the prompt to the model
|
20 |
+
# human_message = HumanMessage(content=prompt)
|
21 |
+
# response = model.invoke([human_message])
|
22 |
+
|
23 |
+
# # Step 3: Return the response from the model
|
24 |
+
# return response.content
|
25 |
+
|
26 |
+
# # Example usage
|
27 |
+
# def gradio_chatbot(input_text):
|
28 |
+
# # Pass the user's input to the chatbot function and get the response
|
29 |
+
# return chatbot_autocorrect_response(input_text)
|
30 |
+
|
31 |
+
# # Launch Gradio interface
|
32 |
+
# interface = gr.Interface(fn=gradio_chatbot, inputs="text", outputs="text", title="Chatbot with Auto-Correction")
|
33 |
+
# interface.launch()
|
34 |
+
|
35 |
import os, getpass
|
36 |
from langchain_openai import ChatOpenAI
|
37 |
from langchain_core.messages import HumanMessage
|
|
|
39 |
import openai
|
40 |
|
41 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
42 |
+
|
43 |
# Define the language model
|
44 |
model = ChatOpenAI(model="gpt-4o-mini")
|
45 |
|
46 |
# Function to generate a conversational response with model-based autocorrection
|
47 |
def chatbot_autocorrect_response(input_text: str):
|
48 |
+
# Define the prompt asking the model to correct the sentence
|
49 |
prompt = (
|
50 |
+
f"The user said: '{input_text}'. Please correct this sentence if necessary, "
|
51 |
+
"and make it sound friendly and casual. Acknowledge the correction and make it sound "
|
52 |
+
"like an American native conversation. If appropriate, make it sound like an IELTS 9.0 level response. "
|
53 |
+
"If sentences are in Indonesian, translate them to sound like native American conversation. "
|
54 |
+
"Please only respond with the corrected sentence. If nothing needs to be changed, repeat the sentence."
|
55 |
+
)
|
56 |
human_message = HumanMessage(content=prompt)
|
57 |
response = model.invoke([human_message])
|
58 |
+
return response.content
|
59 |
|
60 |
+
# Function to provide vocabulary or sentence explanations
|
61 |
+
def chatbot_explanation_response(input_text: str):
|
62 |
+
# Define the prompt to give explanations and examples
|
63 |
+
prompt = (
|
64 |
+
f"The user is asking for a detailed explanation of the following phrase or sentence: '{input_text}'. "
|
65 |
+
"Please provide an explanation of the vocabulary or sentence, including definitions, usage examples, and "
|
66 |
+
"similar expressions or structures. Make it clear and easy to understand, offering alternative ways "
|
67 |
+
"to express the same idea if possible."
|
68 |
+
)
|
69 |
+
human_message = HumanMessage(content=prompt)
|
70 |
+
response = model.invoke([human_message])
|
71 |
return response.content
|
72 |
|
73 |
+
# Gradio interface setup
|
74 |
+
def gradio_chatbot(input_text, explanation_text):
|
75 |
+
# Get responses for autocorrection and explanation
|
76 |
+
autocorrect_response = chatbot_autocorrect_response(input_text)
|
77 |
+
explanation_response = chatbot_explanation_response(explanation_text)
|
78 |
+
return autocorrect_response, explanation_response
|
79 |
|
80 |
+
# Launch Gradio interface with two text inputs
|
81 |
+
interface = gr.Interface(
|
82 |
+
fn=gradio_chatbot,
|
83 |
+
inputs=["text", "text"],
|
84 |
+
outputs=["text", "text"],
|
85 |
+
title="Chatbot with Auto-Correction and Vocabulary Explanations",
|
86 |
+
description="Enter a sentence for autocorrection and another for a vocabulary or sentence explanation."
|
87 |
+
)
|
88 |
+
interface.launch()
|