File size: 1,451 Bytes
4ecd295
 
 
 
 
9542a22
4ecd295
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# Path: chatbot_model_based_autocorrect.py
import os, getpass
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage
import gradio as gr
import openai

openai.api_key = os.getenv("OPENAI_API_KEY")
# Define the language model
model = ChatOpenAI(model="gpt-4o-mini")

# Function to generate a conversational response with model-based autocorrection
def chatbot_autocorrect_response(input_text: str):
    # Step 1: Define the prompt asking the model to correct the sentence
    prompt = (
        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. Please only respond with the corrected sentence. If nothing needs to be changed, repeat the sentence."
)
    
    # Step 2: Send the prompt to the model
    human_message = HumanMessage(content=prompt)
    response = model.invoke([human_message])

    # Step 3: Return the response from the model
    return response.content

# Example usage
def gradio_chatbot(input_text):
    # Pass the user's input to the chatbot function and get the response
    return chatbot_autocorrect_response(input_text)

# Launch Gradio interface
interface = gr.Interface(fn=gradio_chatbot, inputs="text", outputs="text", title="Chatbot with Auto-Correction")
interface.launch()