usmanyousaf commited on
Commit
917a8c3
·
verified ·
1 Parent(s): e3b2421

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -0
app.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from fastapi import FastAPI, File, Form, HTTPException
3
+ from fastapi.responses import JSONResponse
4
+ from pydantic import BaseModel
5
+ from groq import Groq
6
+ import io
7
+
8
+ # Set up the Groq client
9
+ os.environ["GROQ_API_KEY"] = os.getenv("GROQ_API_KEY")
10
+ client = Groq(api_key=os.environ["GROQ_API_KEY"])
11
+
12
+ app = FastAPI()
13
+
14
+ # Pydantic model for the transcription result
15
+ class TranscriptionResponse(BaseModel):
16
+ transcription: str
17
+
18
+ class FeedbackResponse(BaseModel):
19
+ grammar_feedback: str
20
+ vocabulary_feedback: str
21
+ grammar_score: int
22
+ vocabulary_score: int
23
+
24
+ @app.get("/")
25
+ async def index():
26
+ return {"message": "Welcome to the Audio Transcription API!"}
27
+
28
+ @app.post("/transcribe")
29
+ async def transcribe_audio(audio_data: bytes = File(...), language: str = Form(...)):
30
+ try:
31
+ # Transcribe the audio based on the selected language
32
+ transcription = client.audio.transcriptions.create(
33
+ file=("audio.wav", audio_data),
34
+ model="whisper-large-v3",
35
+ prompt="Transcribe the audio accurately based on the selected language.",
36
+ response_format="text",
37
+ language=language,
38
+ )
39
+ return JSONResponse(content=TranscriptionResponse(transcription=transcription).dict())
40
+ except Exception as e:
41
+ raise HTTPException(status_code=500, detail=str(e))
42
+
43
+ @app.post("/check_grammar")
44
+ async def check_grammar(transcription: str = Form(...), language: str = Form(...)):
45
+ if not transcription or not language:
46
+ raise HTTPException(status_code=400, detail="Missing transcription or language selection")
47
+
48
+ try:
49
+ # Grammar check
50
+ grammar_prompt = (
51
+ f"Briefly check the grammar of the following text in {language}: {transcription}. "
52
+ "Identify any word that does not belong to the selected language and flag it. Based on the number of incorrect words, "
53
+ "also check the grammar deeply and carefully. Provide a score from 1 to 10 based on the grammar accuracy, reducing points for incorrect words, "
54
+ "and make sure to output the score on a new line after two line breaks like \"SCORE=\"."
55
+ )
56
+
57
+ grammar_check_response = client.chat.completions.create(
58
+ model="llama3-groq-70b-8192-tool-use-preview",
59
+ messages=[{"role": "user", "content": grammar_prompt}]
60
+ )
61
+ grammar_feedback = grammar_check_response.choices[0].message.content.strip()
62
+
63
+ # Vocabulary check
64
+ vocabulary_prompt = (
65
+ f"Check the vocabulary accuracy of the following text in {language}: {transcription}. "
66
+ "Identify any word that does not belong to the selected language and flag it. Based on the number of incorrect words, "
67
+ "also check the grammar deeply and carefully. Provide a score from 1 to 10 based on the vocabulary accuracy, reducing points for incorrect words, "
68
+ "and make sure to output the score on a new line after two line breaks like \"SCORE=\"."
69
+ )
70
+
71
+ vocabulary_check_response = client.chat.completions.create(
72
+ model="llama-3.1-70b-versatile",
73
+ messages=[{"role": "user", "content": vocabulary_prompt}]
74
+ )
75
+ vocabulary_feedback = vocabulary_check_response.choices[0].message.content.strip()
76
+
77
+ # Calculate scores from feedback
78
+ grammar_score = calculate_score(grammar_feedback)
79
+ vocabulary_score = calculate_score(vocabulary_feedback)
80
+
81
+ return JSONResponse(content=FeedbackResponse(
82
+ grammar_feedback=grammar_feedback,
83
+ vocabulary_feedback=vocabulary_feedback,
84
+ grammar_score=grammar_score,
85
+ vocabulary_score=vocabulary_score
86
+ ).dict())
87
+ except Exception as e:
88
+ raise HTTPException(status_code=500, detail=str(e))
89
+
90
+ def calculate_score(feedback):
91
+ """
92
+ Calculate score based on feedback content.
93
+ This function searches for the keyword 'SCORE=' or similar variations
94
+ (SCORE:, score:, etc.) and extracts the score value.
95
+ """
96
+ import re
97
+ match = re.search(r'(SCORE=|score=|SCORE:|score:|SCORE = )\s*(\d+)', feedback)
98
+
99
+ if match:
100
+ # Extract and return the score as an integer
101
+ return int(match.group(2))
102
+
103
+ # Return a default score of 0 if no score is found in the feedback
104
+ return 0