File tree Expand file tree Collapse file tree 4 files changed +102
-0
lines changed
Language_Learning_Chatbot Expand file tree Collapse file tree 4 files changed +102
-0
lines changed Original file line number Diff line number Diff line change 1+ # Language Learning Chatbot
2+
3+ A conversational AI language learning companion using OpenAI API.
4+
5+ ## Features
6+ - Chatbot for language practice and learning
7+ - Personalized feedback
8+
9+ ## Usage
10+ 1 . Install dependencies:
11+ ``` bash
12+ pip install flask openai
13+ ```
14+ 2 . Set your OpenAI API key as an environment variable:
15+ ``` bash
16+ export OPENAI_API_KEY=sk-...
17+ ```
18+ 3 . Run the Flask app:
19+ ``` bash
20+ python app.py
21+ ```
22+ 4 . Open your browser at ` http://localhost:5000 ` .
23+
24+ ## Requirements
25+ - Python 3.x
26+ - Flask
27+ - OpenAI API key
28+
29+ ## License
30+ MIT
Original file line number Diff line number Diff line change 1+ """
2+ Language Learning Chatbot (Issue #90)
3+ Minimal Flask backend + HTML/JS frontend using OpenAI API
4+ """
5+ from flask import Flask , request , jsonify , send_from_directory
6+ import os
7+ import openai
8+
9+ app = Flask (__name__ )
10+ openai .api_key = os .getenv ("OPENAI_API_KEY" , "sk-..." ) # Replace with your key or set as env var
11+
12+ @app .route ("/api/chat" , methods = ["POST" ])
13+ def chat ():
14+ user_msg = request .json .get ("message" , "" )
15+ prompt = f"You are a helpful language learning assistant. Help the user practice and learn languages.\n User: { user_msg } \n AI:"
16+ response = openai .Completion .create (
17+ engine = "text-davinci-003" ,
18+ prompt = prompt ,
19+ max_tokens = 100 ,
20+ temperature = 0.7
21+ )
22+ reply = response .choices [0 ].text .strip ()
23+ return jsonify ({"reply" : reply })
24+
25+ @app .route ("/" )
26+ def index ():
27+ return send_from_directory ("." , "chatbot.html" )
28+
29+ if __name__ == "__main__" :
30+ app .run (debug = True )
Original file line number Diff line number Diff line change 1+ <!DOCTYPE html>
2+ < html lang ="en ">
3+ < head >
4+ < meta charset ="UTF-8 ">
5+ < title > Language Learning Chatbot</ title >
6+ < style >
7+ body { font-family : Arial, sans-serif; margin : 40px ; }
8+ .container { max-width : 600px ; margin : auto; }
9+ # chat { border : 1px solid # ccc ; padding : 10px ; height : 300px ; overflow-y : auto; margin-bottom : 10px ; }
10+ input , button { margin : 5px ; }
11+ </ style >
12+ </ head >
13+ < body >
14+ < div class ="container ">
15+ < h2 > Language Learning Chatbot</ h2 >
16+ < div id ="chat "> </ div >
17+ < input type ="text " id ="userInput " placeholder ="Type your message... " />
18+ < button onclick ="sendMessage() "> Send</ button >
19+ </ div >
20+ < script >
21+ function appendMessage ( sender , text ) {
22+ const chat = document . getElementById ( 'chat' ) ;
23+ chat . innerHTML += `<b>${ sender } :</b> ${ text } <br/>` ;
24+ chat . scrollTop = chat . scrollHeight ;
25+ }
26+ function sendMessage ( ) {
27+ const input = document . getElementById ( 'userInput' ) ;
28+ const msg = input . value ;
29+ if ( ! msg ) return ;
30+ appendMessage ( 'You' , msg ) ;
31+ input . value = '' ;
32+ fetch ( '/api/chat' , {
33+ method : 'POST' , headers : { 'Content-Type' : 'application/json' } , body : JSON . stringify ( { message : msg } )
34+ } ) . then ( r => r . json ( ) ) . then ( data => {
35+ appendMessage ( 'AI' , data . reply ) ;
36+ } ) ;
37+ }
38+ </ script >
39+ </ body >
40+ </ html >
Original file line number Diff line number Diff line change 1+ flask
2+ openai
You can’t perform that action at this time.
0 commit comments