Update app.py
Browse files
app.py
CHANGED
|
@@ -1,52 +1,51 @@
|
|
| 1 |
import os
|
| 2 |
-
from
|
|
|
|
|
|
|
| 3 |
from groq import Groq
|
|
|
|
| 4 |
|
| 5 |
# Set up the Groq client
|
| 6 |
os.environ["GROQ_API_KEY"] = "your_groq_api_key_here"
|
| 7 |
client = Groq(api_key=os.environ["GROQ_API_KEY"])
|
| 8 |
|
| 9 |
-
app
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
return render_template('index.html')
|
| 15 |
-
|
| 16 |
-
# Route to handle the audio upload and transcription
|
| 17 |
-
@app.route('/transcribe', methods=['POST'])
|
| 18 |
-
def transcribe_audio():
|
| 19 |
-
audio_data = request.files['audio_data']
|
| 20 |
-
selected_language = request.form['language']
|
| 21 |
|
|
|
|
|
|
|
| 22 |
try:
|
|
|
|
| 23 |
# Transcribe the audio based on the selected language
|
| 24 |
transcription = client.audio.transcriptions.create(
|
| 25 |
-
file=(audio_data.filename,
|
| 26 |
model="whisper-large-v3",
|
| 27 |
prompt="Transcribe the audio accurately based on the selected language.",
|
| 28 |
response_format="text",
|
| 29 |
-
language=
|
| 30 |
)
|
| 31 |
|
| 32 |
-
return
|
| 33 |
-
except Exception as e:
|
| 34 |
-
return jsonify({'error': str(e)}), 500
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
def check_grammar():
|
| 39 |
-
transcription = request.form.get('transcription')
|
| 40 |
-
selected_language = request.form.get('language')
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
| 44 |
|
| 45 |
try:
|
| 46 |
# Grammar check
|
| 47 |
grammar_prompt = (
|
| 48 |
-
f"Briefly check the grammar of the following text in {
|
| 49 |
-
"Identify any word that does not belong to the selected language and flag it.
|
|
|
|
| 50 |
"Provide a score from 1 to 10 based on the grammar accuracy, reducing points for incorrect words and make sure to output the score on a new line after two line breaks like 'SCORE='."
|
| 51 |
)
|
| 52 |
|
|
@@ -58,8 +57,9 @@ def check_grammar():
|
|
| 58 |
|
| 59 |
# Vocabulary check
|
| 60 |
vocabulary_prompt = (
|
| 61 |
-
f"Check the vocabulary accuracy of the following text in {
|
| 62 |
-
"Identify any word that does not belong to the selected language and flag it.
|
|
|
|
| 63 |
"Provide a score from 1 to 10 based on the vocabulary accuracy reducing points for incorrect words and make sure to output the score on a new line after two line breaks like 'SCORE='."
|
| 64 |
)
|
| 65 |
|
|
@@ -69,22 +69,24 @@ def check_grammar():
|
|
| 69 |
)
|
| 70 |
vocabulary_feedback = vocabulary_check_response.choices[0].message.content.strip()
|
| 71 |
|
| 72 |
-
#
|
| 73 |
grammar_score = calculate_score(grammar_feedback)
|
| 74 |
vocabulary_score = calculate_score(vocabulary_feedback)
|
| 75 |
|
| 76 |
-
return
|
| 77 |
'grammar_feedback': grammar_feedback,
|
| 78 |
'vocabulary_feedback': vocabulary_feedback,
|
| 79 |
'grammar_score': grammar_score,
|
| 80 |
'vocabulary_score': vocabulary_score
|
| 81 |
})
|
|
|
|
| 82 |
except Exception as e:
|
| 83 |
-
return
|
|
|
|
| 84 |
|
| 85 |
-
def calculate_score(feedback):
|
| 86 |
"""
|
| 87 |
-
Calculate score based on feedback content.
|
| 88 |
This function searches for the keyword 'SCORE=' or similar variations
|
| 89 |
(SCORE:, score:, etc.) and extracts the score value.
|
| 90 |
"""
|
|
@@ -92,11 +94,12 @@ def calculate_score(feedback):
|
|
| 92 |
match = re.search(r'(SCORE=|score=|SCORE:|score:|SCORE = )\s*(\d+)', feedback)
|
| 93 |
|
| 94 |
if match:
|
| 95 |
-
# Extract and return the score as an integer
|
| 96 |
return int(match.group(2))
|
| 97 |
|
| 98 |
-
# Return
|
| 99 |
-
|
| 100 |
|
|
|
|
| 101 |
if __name__ == "__main__":
|
| 102 |
-
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
from fastapi import FastAPI, Request, Form, File, UploadFile
|
| 3 |
+
from fastapi.responses import HTMLResponse, JSONResponse
|
| 4 |
+
from fastapi.templating import Jinja2Templates
|
| 5 |
from groq import Groq
|
| 6 |
+
import io
|
| 7 |
|
| 8 |
# Set up the Groq client
|
| 9 |
os.environ["GROQ_API_KEY"] = "your_groq_api_key_here"
|
| 10 |
client = Groq(api_key=os.environ["GROQ_API_KEY"])
|
| 11 |
|
| 12 |
+
# Initialize FastAPI app and template engine
|
| 13 |
+
app = FastAPI()
|
| 14 |
+
templates = Jinja2Templates(directory="templates")
|
| 15 |
|
| 16 |
+
@app.get("/", response_class=HTMLResponse)
|
| 17 |
+
async def index(request: Request):
|
| 18 |
+
return templates.TemplateResponse("index.html", {"request": request})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
@app.post("/transcribe")
|
| 21 |
+
async def transcribe_audio(audio_data: UploadFile = File(...), language: str = Form(...)):
|
| 22 |
try:
|
| 23 |
+
audio_content = await audio_data.read()
|
| 24 |
# Transcribe the audio based on the selected language
|
| 25 |
transcription = client.audio.transcriptions.create(
|
| 26 |
+
file=(audio_data.filename, audio_content),
|
| 27 |
model="whisper-large-v3",
|
| 28 |
prompt="Transcribe the audio accurately based on the selected language.",
|
| 29 |
response_format="text",
|
| 30 |
+
language=language,
|
| 31 |
)
|
| 32 |
|
| 33 |
+
return JSONResponse(content={'transcription': transcription})
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
except Exception as e:
|
| 36 |
+
return JSONResponse(status_code=500, content={'error': str(e)})
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
@app.post("/check_grammar")
|
| 39 |
+
async def check_grammar(transcription: str = Form(...), language: str = Form(...)):
|
| 40 |
+
if not transcription or not language:
|
| 41 |
+
return JSONResponse(status_code=400, content={'error': 'Missing transcription or language selection'})
|
| 42 |
|
| 43 |
try:
|
| 44 |
# Grammar check
|
| 45 |
grammar_prompt = (
|
| 46 |
+
f"Briefly check the grammar of the following text in {language}: {transcription}. "
|
| 47 |
+
"Identify any word that does not belong to the selected language and flag it. "
|
| 48 |
+
"Based on the number of incorrect words also check the grammar deeply and carefully. "
|
| 49 |
"Provide a score from 1 to 10 based on the grammar accuracy, reducing points for incorrect words and make sure to output the score on a new line after two line breaks like 'SCORE='."
|
| 50 |
)
|
| 51 |
|
|
|
|
| 57 |
|
| 58 |
# Vocabulary check
|
| 59 |
vocabulary_prompt = (
|
| 60 |
+
f"Check the vocabulary accuracy of the following text in {language}: {transcription}. "
|
| 61 |
+
"Identify any word that does not belong to the selected language and flag it. "
|
| 62 |
+
"Based on the number of incorrect words also check the grammar deeply and carefully. "
|
| 63 |
"Provide a score from 1 to 10 based on the vocabulary accuracy reducing points for incorrect words and make sure to output the score on a new line after two line breaks like 'SCORE='."
|
| 64 |
)
|
| 65 |
|
|
|
|
| 69 |
)
|
| 70 |
vocabulary_feedback = vocabulary_check_response.choices[0].message.content.strip()
|
| 71 |
|
| 72 |
+
# Calculate scores
|
| 73 |
grammar_score = calculate_score(grammar_feedback)
|
| 74 |
vocabulary_score = calculate_score(vocabulary_feedback)
|
| 75 |
|
| 76 |
+
return JSONResponse(content={
|
| 77 |
'grammar_feedback': grammar_feedback,
|
| 78 |
'vocabulary_feedback': vocabulary_feedback,
|
| 79 |
'grammar_score': grammar_score,
|
| 80 |
'vocabulary_score': vocabulary_score
|
| 81 |
})
|
| 82 |
+
|
| 83 |
except Exception as e:
|
| 84 |
+
return JSONResponse(status_code=500, content={'error': str(e)})
|
| 85 |
+
|
| 86 |
|
| 87 |
+
def calculate_score(feedback: str) -> int:
|
| 88 |
"""
|
| 89 |
+
Calculate score based on feedback content.
|
| 90 |
This function searches for the keyword 'SCORE=' or similar variations
|
| 91 |
(SCORE:, score:, etc.) and extracts the score value.
|
| 92 |
"""
|
|
|
|
| 94 |
match = re.search(r'(SCORE=|score=|SCORE:|score:|SCORE = )\s*(\d+)', feedback)
|
| 95 |
|
| 96 |
if match:
|
|
|
|
| 97 |
return int(match.group(2))
|
| 98 |
|
| 99 |
+
return 0 # Return default score of 0 if no score is found
|
| 100 |
+
|
| 101 |
|
| 102 |
+
# Run the FastAPI app (only needed for local development)
|
| 103 |
if __name__ == "__main__":
|
| 104 |
+
import uvicorn
|
| 105 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|