usmanyousaf commited on
Commit
30432c9
·
verified ·
1 Parent(s): 0cfaddf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -76
app.py CHANGED
@@ -1,104 +1,92 @@
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
 
1
  import os
2
+ from flask import Flask, request, render_template, jsonify
 
 
3
  from groq import Groq
4
+ import json
5
 
6
  # Set up the Groq client
7
+ os.environ["GROQ_API_KEY"] = "your_groq_api_key_here" # Replace with your actual API key
8
  client = Groq(api_key=os.environ["GROQ_API_KEY"])
9
 
10
+ app = Flask(__name__)
11
 
12
+ # Route to render the upload form
13
+ @app.route('/')
14
+ def index():
15
+ return render_template('index.html')
16
 
17
+ # Route to handle the audio upload and transcription
18
+ @app.route('/transcribe', methods=['POST'])
19
+ def transcribe_audio():
20
+ audio_data = request.files['audio_data']
21
+ selected_language = request.form['language']
22
 
 
 
 
 
 
 
23
  try:
24
  # Transcribe the audio based on the selected language
25
  transcription = client.audio.transcriptions.create(
26
+ file=(audio_data.filename, audio_data.read()),
27
+ model="whisper-large-v3", # You can change this model if needed
 
28
  response_format="text",
29
+ language=selected_language,
30
  )
31
+
32
+ return jsonify({'transcription': transcription['text']}) # Fix to extract text
33
  except Exception as e:
34
+ return str(e), 500
35
 
36
+ # Route to check grammar and vocabulary of the transcription
37
+ @app.route('/check_grammar', methods=['POST'])
38
+ def check_grammar():
39
+ transcription = request.form['transcription']
40
+ selected_language = request.form['language']
41
 
42
+ if not transcription:
43
+ return "No transcription available."
 
 
 
 
 
 
44
 
45
+ try:
46
+ # Grammar check with scoring and concise feedback
47
+ grammar_prompt = f"""
48
+ Check the grammar of the following text in {selected_language}:
49
+ \"\"\"
50
+ {transcription}
51
+ \"\"\"
52
+ Provide a score from 1 to 10 and a concise feedback (1-1.5 lines) without including the score in the feedback.
53
+ Return the result in JSON format with keys 'score' and 'feedback'.
54
+ """
55
  grammar_check_response = client.chat.completions.create(
56
+ model="gemma-7b-it", # Change the model if needed
57
  messages=[{"role": "user", "content": grammar_prompt}]
58
  )
59
+ grammar_response_content = grammar_check_response.choices[0].message.content.strip()
60
+ grammar_result = json.loads(grammar_response_content)
61
+ grammar_score = grammar_result.get('score', '')
62
+ grammar_feedback = grammar_result.get('feedback', '')
 
 
 
 
 
63
 
64
+ # Vocabulary check with scoring and concise feedback
65
+ vocabulary_prompt = f"""
66
+ Check the vocabulary accuracy of the following text in {selected_language}:
67
+ \"\"\"
68
+ {transcription}
69
+ \"\"\"
70
+ Provide a score from 1 to 10 and a concise feedback (1-1.5 lines) without including the score in the feedback.
71
+ Return the result in JSON format with keys 'score' and 'feedback'.
72
+ """
73
  vocabulary_check_response = client.chat.completions.create(
74
+ model="llama3-8b-8192", # Change the model if needed
75
  messages=[{"role": "user", "content": vocabulary_prompt}]
76
  )
77
+ vocabulary_response_content = vocabulary_check_response.choices[0].message.content.strip()
78
+ vocabulary_result = json.loads(vocabulary_response_content)
79
+ vocabulary_score = vocabulary_result.get('score', '')
80
+ vocabulary_feedback = vocabulary_result.get('feedback', '')
 
81
 
82
+ return jsonify({
83
+ 'grammar_feedback': grammar_feedback,
84
+ 'grammar_score': grammar_score,
85
+ 'vocabulary_feedback': vocabulary_feedback,
86
+ 'vocabulary_score': vocabulary_score
87
+ })
88
  except Exception as e:
89
+ return str(e), 500
90
 
91
+ if __name__ == "__main__":
92
+ app.run(debug=True)