File size: 2,037 Bytes
8eb2cb0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from flask import Flask, render_template, request, jsonify
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, pipeline
from mistral_7b import generate_text 
import torch  
from inference import voice_inference


app = Flask(__name__, static_url_path='/static')

# ๋ชจ๋ธ ๋ฐ ํ† ํฌ๋‚˜์ด์ € ๋กœ๋“œ
tokenizer_name = "gogamza/kobart-base-v2"
tokenizer = AutoTokenizer.from_pretrained(tokenizer_name)  # ํ† ํฌ๋‚˜์ด์ € ์ˆ˜์ •

model_name = "/content/flask/eojin/checkpoint-142243"
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)  # ๋ชจ๋ธ ์ˆ˜์ •

# Pipeline์„ ์ด์šฉํ•ด์„œ ํ•™์Šตํ•œ ๋ชจ๋ธ๋กœ ํ…์ŠคํŠธ ์ƒ์„ฑํ•ด๋ณด๊ธฐ
nlg_pipeline = pipeline('translation_ko_to_ko', model=model, tokenizer=tokenizer)

@app.route('/')
def index():
    return render_template("index.html")

@app.route('/voice/<sentence>')
def voice(sentence):
    OUTPUT_WAV_PATH = voice_inference(sentence)
    return OUTPUT_WAV_PATH

@app.route('/chatbot')
def chatbot():
    return render_template("chatbot.html")

# ์ž…๋ ฅ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒฝ๋กœ
@app.route('/process_input/<input_text>')
def process_input(input_text):
    try:
        print("input_text", input_text, "==========================================")
        
        # answer ๋ฐ›๊ธฐ
        with torch.no_grad():
            answer = generate_text(input_text)
        
        print(answer, "=============================================")
        
        # ํ…์ŠคํŠธ ๋งํˆฌ ๋ณ€ํ™˜ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ œ์ฃผ๋„ ์‚ฌํˆฌ๋ฆฌ๋กœ ๋ณ€ํ™˜
        jeju_answer = nlg_pipeline(answer, max_length=60)[0]['translation_text']

        print(jeju_answer, "=============================================")
        
        # ๊ฒฐ๊ณผ๋ฅผ JSON ํ˜•์‹์œผ๋กœ ๋ฐ˜ํ™˜
        return jsonify({'answer': answer, 'jeju_answer': jeju_answer})
    
    except Exception as e:
        print("Exception:", str(e))
        # ์˜ˆ์™ธ ๋ฐœ์ƒ ์‹œ ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋ฅผ JSON ํ˜•์‹์œผ๋กœ ๋ฐ˜ํ™˜
        return jsonify({'error': str(e)})


if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=8000)