case_study / utils.py
GurgenGulay's picture
Update utils.py
c2386f3 verified
raw
history blame
1.82 kB
import logging
from transformers import pipeline
# Logging Ayarları
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
logger = logging.getLogger(__name__)
pipe = pipeline("text2text-generation", model="google-t5/t5-base", device="cpu")
pipe.model.config.pad_token_id = pipe.tokenizer.eos_token_id
def generate_lesson_from_transcript(doc_text):
try:
logger.info("Generating lesson from transcript.")
generated_text = pipe(doc_text, max_length=100, truncation=True)[0]['generated_text']
output_path = "/tmp/generated_output.txt"
with open(output_path, "w") as file:
file.write(generated_text)
logger.info(f"Lesson generation successful. Output saved at: {output_path}")
return generated_text, output_path
except Exception as e:
logger.error(f"Error occurred during lesson generation: {str(e)}")
return "An error occurred", None
def split_text_into_chunks(text, chunk_size=1000):
words = text.split()
chunks = []
for i in range(0, len(words), chunk_size):
chunk = ' '.join(words[i:i+chunk_size])
chunks.append(chunk)
return chunks
def generate_lesson_from_chunks(chunks):
generated_texts = []
for chunk in chunks:
try:
generated_text = pipe(chunk, max_length=500, truncation=True)[0]['generated_text']
generated_texts.append(generated_text)
except Exception as e:
print(f"Error in chunk processing: {str(e)}")
continue # Hata durumunda işlemi sürdür
return ' '.join(generated_texts)
def process_large_text(text):
chunks = split_text_into_chunks(text, chunk_size=1000)
generated_text = generate_lesson_from_chunks(chunks)
return generated_text