File size: 4,882 Bytes
917a8c3 bdb0427 917a8c3 bdb0427 917a8c3 aab3cd1 917a8c3 bdb0427 917a8c3 bdb0427 917a8c3 2020dda bdb0427 917a8c3 2020dda bdb0427 2020dda 917a8c3 bdb0427 c60e75c 917a8c3 bdb0427 917a8c3 30432c9 2020dda bdb0427 917a8c3 bdb0427 2020dda bdb0427 917a8c3 bdb0427 917a8c3 30432c9 c60e75c bdb0427 c60e75c 917a8c3 c60e75c 917a8c3 c60e75c bdb0427 c60e75c 917a8c3 c60e75c 917a8c3 c60e75c bdb0427 c60e75c 917a8c3 bdb0427 30432c9 c60e75c 30432c9 bdb0427 917a8c3 bdb0427 c60e75c bdb0427 c60e75c bdb0427 c60e75c bdb0427 917a8c3 bdb0427 92a2280 30432c9 bdb0427 92a2280 |
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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
import os
from fastapi import FastAPI, Request, Form, File, UploadFile
from fastapi.responses import HTMLResponse, JSONResponse
from fastapi.templating import Jinja2Templates
from groq import Groq
import io
# Set up the Groq client
os.environ["GROQ_API_KEY"] = "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"
client = Groq(api_key=os.environ["GROQ_API_KEY"])
# Initialize FastAPI app and template engine
app = FastAPI()
templates = Jinja2Templates(directory="templates")
@app.get("/", response_class=HTMLResponse)
async def index(request: Request):
return templates.TemplateResponse("index.html", {"request": request})
import logging
# Set up basic logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
@app.post("/transcribe")
async def transcribe_audio(audio_data: UploadFile = File(...), language: str = Form(...)):
try:
logger.debug(f"Received audio file: {audio_data.filename} with language: {language}")
audio_content = await audio_data.read()
logger.debug(f"Audio content length: {len(audio_content)}")
# Transcribe the audio based on the selected language
transcription = client.audio.transcriptions.create(
file=(audio_data.filename, audio_content),
model="whisper-large-v3",
prompt="Transcribe the audio accurately based on the selected language.",
response_format="text",
language=language,
)
logger.debug(f"Transcription result: {transcription}")
return JSONResponse(content={'transcription': transcription})
except Exception as e:
logger.error(f"Error during transcription: {e}")
return JSONResponse(status_code=500, content={'error': str(e)})
@app.post("/check_grammar")
async def check_grammar(transcription: str = Form(...), language: str = Form(...)):
if not transcription or not language:
return JSONResponse(status_code=400, content={'error': 'Missing transcription or language selection'})
try:
# Grammar check
grammar_prompt = (
f"Briefly check the grammar of the following text in {language}: {transcription}. "
"Identify any word that does not belong to the selected language and flag it. "
"Based on the number of incorrect words also check the grammar deeply and carefully. "
"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='."
)
grammar_check_response = client.chat.completions.create(
model="llama3-groq-70b-8192-tool-use-preview",
messages=[{"role": "user", "content": grammar_prompt}]
)
grammar_feedback = grammar_check_response.choices[0].message.content.strip()
# Vocabulary check
vocabulary_prompt = (
f"Check the vocabulary accuracy of the following text in {language}: {transcription}. "
"Identify any word that does not belong to the selected language and flag it. "
"Based on the number of incorrect words also check the grammar deeply and carefully. "
"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='."
)
vocabulary_check_response = client.chat.completions.create(
model="llama-3.1-70b-versatile",
messages=[{"role": "user", "content": vocabulary_prompt}]
)
vocabulary_feedback = vocabulary_check_response.choices[0].message.content.strip()
# Calculate scores
grammar_score = calculate_score(grammar_feedback)
vocabulary_score = calculate_score(vocabulary_feedback)
return JSONResponse(content={
'grammar_feedback': grammar_feedback,
'vocabulary_feedback': vocabulary_feedback,
'grammar_score': grammar_score,
'vocabulary_score': vocabulary_score
})
except Exception as e:
return JSONResponse(status_code=500, content={'error': str(e)})
def calculate_score(feedback: str) -> int:
"""
Calculate score based on feedback content.
This function searches for the keyword 'SCORE=' or similar variations
(SCORE:, score:, etc.) and extracts the score value.
"""
import re
match = re.search(r'(SCORE=|score=|SCORE:|score:|SCORE = )\s*(\d+)', feedback)
if match:
return int(match.group(2))
return 0 # Return default score of 0 if no score is found
# Run the FastAPI app (only needed for local development)
# Run the FastAPI app (needed for local development)
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=7860)
|