Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,10 +7,7 @@ import tempfile
|
|
| 7 |
from nltk.tokenize import sent_tokenize
|
| 8 |
import random
|
| 9 |
from groq import Groq
|
| 10 |
-
|
| 11 |
-
# Ensure no unexpected indentation here
|
| 12 |
api_key = os.environ.get("GROQ_API_KEY")
|
| 13 |
-
|
| 14 |
# Attempt to download punkt tokenizer
|
| 15 |
try:
|
| 16 |
nltk.download("punkt")
|
|
@@ -26,7 +23,7 @@ def transcribe(audio_path):
|
|
| 26 |
|
| 27 |
groq_api_endpoint = "https://api.groq.com/openai/v1/audio/transcriptions"
|
| 28 |
headers = {
|
| 29 |
-
|
| 30 |
}
|
| 31 |
files = {
|
| 32 |
'file': ('audio.wav', audio_data, 'audio/wav'),
|
|
@@ -49,34 +46,91 @@ def transcribe(audio_path):
|
|
| 49 |
return create_error_pdf(f"API Error: {error_msg}")
|
| 50 |
|
| 51 |
def generate_notes(transcript):
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
chat_completion = client.chat.completions.create(
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
|
|
|
| 75 |
# Generate and save a structured PDF
|
| 76 |
-
pdf_path = create_pdf(res,
|
| 77 |
return pdf_path
|
| 78 |
|
| 79 |
-
def create_pdf(question,
|
| 80 |
pdf = FPDF()
|
| 81 |
pdf.add_page()
|
| 82 |
|
|
@@ -95,6 +149,23 @@ def create_pdf(question, transcript):
|
|
| 95 |
|
| 96 |
pdf.multi_cell(0, 10, f"- {question.encode('latin1', 'replace').decode('latin1')}\n")
|
| 97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_pdf:
|
| 99 |
pdf.output(temp_pdf.name)
|
| 100 |
pdf_path = temp_pdf.name
|
|
@@ -123,4 +194,4 @@ iface = gr.Interface(
|
|
| 123 |
description="This app converts audio to text and generates academic questions including long, short, and multiple-choice questions."
|
| 124 |
)
|
| 125 |
|
| 126 |
-
iface.launch()
|
|
|
|
| 7 |
from nltk.tokenize import sent_tokenize
|
| 8 |
import random
|
| 9 |
from groq import Groq
|
|
|
|
|
|
|
| 10 |
api_key = os.environ.get("GROQ_API_KEY")
|
|
|
|
| 11 |
# Attempt to download punkt tokenizer
|
| 12 |
try:
|
| 13 |
nltk.download("punkt")
|
|
|
|
| 23 |
|
| 24 |
groq_api_endpoint = "https://api.groq.com/openai/v1/audio/transcriptions"
|
| 25 |
headers = {
|
| 26 |
+
"Authorization": f"Bearer {api_key}", # Fix: api_key is used properly
|
| 27 |
}
|
| 28 |
files = {
|
| 29 |
'file': ('audio.wav', audio_data, 'audio/wav'),
|
|
|
|
| 46 |
return create_error_pdf(f"API Error: {error_msg}")
|
| 47 |
|
| 48 |
def generate_notes(transcript):
|
| 49 |
+
# try:
|
| 50 |
+
# sentences = sent_tokenize(transcript)
|
| 51 |
+
# except LookupError:
|
| 52 |
+
# sentences = custom_sent_tokenize(transcript)
|
| 53 |
+
|
| 54 |
+
# # Generate long questions
|
| 55 |
+
# long_questions = [f"Explain the concept discussed in: '{sentence}'." for sentence in sentences[:5]]
|
| 56 |
+
|
| 57 |
+
# # Generate short questions
|
| 58 |
+
# short_questions = [f"What does '{sentence.split()[0]}' mean in the context of this text?" for sentence in sentences[:5]]
|
| 59 |
+
|
| 60 |
+
# # Generate MCQs with relevant distractors
|
| 61 |
+
# mcqs = []
|
| 62 |
+
# for sentence in sentences[:5]:
|
| 63 |
+
# if len(sentence.split()) > 1: # Ensure there are enough words to create meaningful options
|
| 64 |
+
# key_word = sentence.split()[0] # Use the first word as a key term
|
| 65 |
+
# distractors = ["Term A", "Term B", "Term C"] # Replace with relevant terms if needed
|
| 66 |
+
# options = [key_word] + distractors
|
| 67 |
+
# random.shuffle(options) # Shuffle options for randomness
|
| 68 |
+
# mcq = {
|
| 69 |
+
# "question": f"What is '{key_word}' based on the context?",
|
| 70 |
+
# "options": options,
|
| 71 |
+
# "answer": key_word
|
| 72 |
+
# }
|
| 73 |
+
# mcqs.append(mcq)
|
| 74 |
+
client = Groq(api_key="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")
|
| 75 |
|
| 76 |
chat_completion = client.chat.completions.create(
|
| 77 |
+
#
|
| 78 |
+
# Required parameters
|
| 79 |
+
#
|
| 80 |
+
messages=[
|
| 81 |
+
# Set an optional system message. This sets the behavior of the
|
| 82 |
+
# assistant and can be used to provide specific instructions for
|
| 83 |
+
# how it should behave throughout the conversation.
|
| 84 |
+
{
|
| 85 |
+
"role": "system",
|
| 86 |
+
"content": "you are expert question generator from content. Generate one long question,possible number of short questions and mcqs.plz also provide the notes"
|
| 87 |
+
},
|
| 88 |
+
# Set a user message for the assistant to respond to.
|
| 89 |
+
{
|
| 90 |
+
"role": "user",
|
| 91 |
+
"content": transcript,
|
| 92 |
+
}
|
| 93 |
+
],
|
| 94 |
+
|
| 95 |
+
# The language model which will generate the completion.
|
| 96 |
+
model="llama3-8b-8192",
|
| 97 |
+
|
| 98 |
+
#
|
| 99 |
+
# Optional parameters
|
| 100 |
+
#
|
| 101 |
+
|
| 102 |
+
# Controls randomness: lowering results in less random completions.
|
| 103 |
+
# As the temperature approaches zero, the model will become deterministic
|
| 104 |
+
# and repetitive.
|
| 105 |
+
temperature=0.5,
|
| 106 |
+
|
| 107 |
+
# The maximum number of tokens to generate. Requests can use up to
|
| 108 |
+
# 32,768 tokens shared between prompt and completion.
|
| 109 |
+
max_tokens=1024,
|
| 110 |
+
|
| 111 |
+
# Controls diversity via nucleus sampling: 0.5 means half of all
|
| 112 |
+
# likelihood-weighted options are considered.
|
| 113 |
+
top_p=1,
|
| 114 |
+
|
| 115 |
+
# A stop sequence is a predefined or user-specified text string that
|
| 116 |
+
# signals an AI to stop generating content, ensuring its responses
|
| 117 |
+
# remain focused and concise. Examples include punctuation marks and
|
| 118 |
+
# markers like "[end]".
|
| 119 |
+
stop=None,
|
| 120 |
+
|
| 121 |
+
# If set, partial message deltas will be sent.
|
| 122 |
+
stream=False,
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
# Print the completion returned by the LLM.
|
| 126 |
+
res=chat_completion.choices[0].message.content
|
| 127 |
|
| 128 |
+
|
| 129 |
# Generate and save a structured PDF
|
| 130 |
+
pdf_path = create_pdf(res,transcript)
|
| 131 |
return pdf_path
|
| 132 |
|
| 133 |
+
def create_pdf(question,transcript):
|
| 134 |
pdf = FPDF()
|
| 135 |
pdf.add_page()
|
| 136 |
|
|
|
|
| 149 |
|
| 150 |
pdf.multi_cell(0, 10, f"- {question.encode('latin1', 'replace').decode('latin1')}\n")
|
| 151 |
|
| 152 |
+
# # Add short questions
|
| 153 |
+
# pdf.set_font("Arial", "B", 14)
|
| 154 |
+
# pdf.cell(200, 10, "Short Questions", ln=True)
|
| 155 |
+
# pdf.set_font("Arial", "", 12)
|
| 156 |
+
# for question in short_questions:
|
| 157 |
+
# pdf.multi_cell(0, 10, f"- {question.encode('latin1', 'replace').decode('latin1')}\n")
|
| 158 |
+
|
| 159 |
+
# # Add MCQs
|
| 160 |
+
# pdf.set_font("Arial", "B", 14)
|
| 161 |
+
# pdf.cell(200, 10, "Multiple Choice Questions (MCQs)", ln=True)
|
| 162 |
+
# pdf.set_font("Arial", "", 12)
|
| 163 |
+
# for mcq in mcqs:
|
| 164 |
+
# pdf.multi_cell(0, 10, f"Q: {mcq['question'].encode('latin1', 'replace').decode('latin1')}")
|
| 165 |
+
# for option in mcq["options"]:
|
| 166 |
+
# pdf.multi_cell(0, 10, f" - {option.encode('latin1', 'replace').decode('latin1')}")
|
| 167 |
+
# pdf.multi_cell(0, 10, f"Answer: {mcq['answer'].encode('latin1', 'replace').decode('latin1')}\n")
|
| 168 |
+
|
| 169 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_pdf:
|
| 170 |
pdf.output(temp_pdf.name)
|
| 171 |
pdf_path = temp_pdf.name
|
|
|
|
| 194 |
description="This app converts audio to text and generates academic questions including long, short, and multiple-choice questions."
|
| 195 |
)
|
| 196 |
|
| 197 |
+
iface.launch()
|