File size: 3,611 Bytes
917a8c3
30432c9
917a8c3
30432c9
917a8c3
 
30432c9
917a8c3
 
30432c9
917a8c3
30432c9
 
 
 
917a8c3
30432c9
 
 
 
 
917a8c3
 
 
 
30432c9
 
917a8c3
30432c9
917a8c3
30432c9
 
917a8c3
30432c9
917a8c3
30432c9
 
 
 
 
917a8c3
30432c9
 
917a8c3
30432c9
 
 
 
 
 
 
 
 
 
917a8c3
30432c9
917a8c3
 
30432c9
 
 
 
917a8c3
30432c9
 
 
 
 
 
 
 
 
917a8c3
30432c9
917a8c3
 
30432c9
 
 
 
917a8c3
30432c9
 
 
 
 
 
917a8c3
30432c9
917a8c3
30432c9
 
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import os
from flask import Flask, request, render_template, jsonify
from groq import Groq
import json

# Set up the Groq client
os.environ["GROQ_API_KEY"] = "your_groq_api_key_here"  # Replace with your actual API key
client = Groq(api_key=os.environ["GROQ_API_KEY"])

app = Flask(__name__)

# Route to render the upload form
@app.route('/')
def index():
    return render_template('index.html')

# Route to handle the audio upload and transcription
@app.route('/transcribe', methods=['POST'])
def transcribe_audio():
    audio_data = request.files['audio_data']
    selected_language = request.form['language']

    try:
        # Transcribe the audio based on the selected language
        transcription = client.audio.transcriptions.create(
            file=(audio_data.filename, audio_data.read()),
            model="whisper-large-v3",  # You can change this model if needed
            response_format="text",
            language=selected_language,
        )

        return jsonify({'transcription': transcription['text']})  # Fix to extract text
    except Exception as e:
        return str(e), 500

# Route to check grammar and vocabulary of the transcription
@app.route('/check_grammar', methods=['POST'])
def check_grammar():
    transcription = request.form['transcription']
    selected_language = request.form['language']

    if not transcription:
        return "No transcription available."

    try:
        # Grammar check with scoring and concise feedback
        grammar_prompt = f"""
        Check the grammar of the following text in {selected_language}:
        \"\"\"
        {transcription}
        \"\"\"
        Provide a score from 1 to 10 and a concise feedback (1-1.5 lines) without including the score in the feedback.
        Return the result in JSON format with keys 'score' and 'feedback'.
        """
        grammar_check_response = client.chat.completions.create(
            model="gemma-7b-it",  # Change the model if needed
            messages=[{"role": "user", "content": grammar_prompt}]
        )
        grammar_response_content = grammar_check_response.choices[0].message.content.strip()
        grammar_result = json.loads(grammar_response_content)
        grammar_score = grammar_result.get('score', '')
        grammar_feedback = grammar_result.get('feedback', '')

        # Vocabulary check with scoring and concise feedback
        vocabulary_prompt = f"""
        Check the vocabulary accuracy of the following text in {selected_language}:
        \"\"\"
        {transcription}
        \"\"\"
        Provide a score from 1 to 10 and a concise feedback (1-1.5 lines) without including the score in the feedback.
        Return the result in JSON format with keys 'score' and 'feedback'.
        """
        vocabulary_check_response = client.chat.completions.create(
            model="llama3-8b-8192",  # Change the model if needed
            messages=[{"role": "user", "content": vocabulary_prompt}]
        )
        vocabulary_response_content = vocabulary_check_response.choices[0].message.content.strip()
        vocabulary_result = json.loads(vocabulary_response_content)
        vocabulary_score = vocabulary_result.get('score', '')
        vocabulary_feedback = vocabulary_result.get('feedback', '')

        return jsonify({
            'grammar_feedback': grammar_feedback,
            'grammar_score': grammar_score,
            'vocabulary_feedback': vocabulary_feedback,
            'vocabulary_score': vocabulary_score
        })
    except Exception as e:
        return str(e), 500

if __name__ == "__main__":
    app.run(debug=True)