Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,8 +2,9 @@ import os
|
|
| 2 |
import json
|
| 3 |
import requests
|
| 4 |
import nltk
|
|
|
|
| 5 |
from nltk.corpus import stopwords
|
| 6 |
-
from nltk.tokenize import word_tokenize
|
| 7 |
import gradio as gr
|
| 8 |
from datetime import datetime
|
| 9 |
from bs4 import BeautifulSoup
|
|
@@ -99,32 +100,38 @@ def log_interaction(data: dict):
|
|
| 99 |
print(f"Logging failed: {e}")
|
| 100 |
|
| 101 |
|
| 102 |
-
def
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
|
|
|
| 120 |
else:
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
return "\n\n".join(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
|
| 130 |
def generate_summary_and_roi(text):
|
|
@@ -162,8 +169,9 @@ def course_agent(name, email, location, profession, query):
|
|
| 162 |
course_text = extract_course_text_from_html(course_url)
|
| 163 |
|
| 164 |
if course_text:
|
| 165 |
-
introduction =
|
| 166 |
summary_and_roi = generate_summary_and_roi(course_text)
|
|
|
|
| 167 |
result = (
|
| 168 |
f"### Introduction\n{introduction}\n\n"
|
| 169 |
f"### Course URL\n{course_url}\n\n"
|
|
|
|
| 2 |
import json
|
| 3 |
import requests
|
| 4 |
import nltk
|
| 5 |
+
import re
|
| 6 |
from nltk.corpus import stopwords
|
| 7 |
+
from nltk.tokenize import word_tokenize, sent_tokenize
|
| 8 |
import gradio as gr
|
| 9 |
from datetime import datetime
|
| 10 |
from bs4 import BeautifulSoup
|
|
|
|
| 100 |
print(f"Logging failed: {e}")
|
| 101 |
|
| 102 |
|
| 103 |
+
def clean_and_split_intro(text, max_words=750):
|
| 104 |
+
# Replace multiple spaces/newlines by single space
|
| 105 |
+
cleaned = re.sub(r'\s+', ' ', text).strip()
|
| 106 |
+
|
| 107 |
+
# Split into sentences
|
| 108 |
+
sentences = sent_tokenize(cleaned)
|
| 109 |
+
|
| 110 |
+
paragraphs = []
|
| 111 |
+
current_paragraph = []
|
| 112 |
+
current_word_count = 0
|
| 113 |
+
|
| 114 |
+
for sent in sentences:
|
| 115 |
+
sent_word_count = len(sent.split())
|
| 116 |
+
if current_word_count + sent_word_count > max_words / 4: # ~4 paragraphs max in 750 words
|
| 117 |
+
paragraphs.append(' '.join(current_paragraph))
|
| 118 |
+
current_paragraph = [sent]
|
| 119 |
+
current_word_count = sent_word_count
|
| 120 |
+
if len(paragraphs) >= 4:
|
| 121 |
+
break
|
| 122 |
else:
|
| 123 |
+
current_paragraph.append(sent)
|
| 124 |
+
current_word_count += sent_word_count
|
| 125 |
+
|
| 126 |
+
if current_paragraph and len(paragraphs) < 5:
|
| 127 |
+
paragraphs.append(' '.join(current_paragraph))
|
| 128 |
+
|
| 129 |
+
return "\n\n".join(paragraphs)
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
def generate_structured_intro(text):
|
| 133 |
+
# Clean and split intro nicely (max 500 words)
|
| 134 |
+
return clean_and_split_intro(text, max_words=750)
|
| 135 |
|
| 136 |
|
| 137 |
def generate_summary_and_roi(text):
|
|
|
|
| 169 |
course_text = extract_course_text_from_html(course_url)
|
| 170 |
|
| 171 |
if course_text:
|
| 172 |
+
introduction = generate_structured_intro(course_text)
|
| 173 |
summary_and_roi = generate_summary_and_roi(course_text)
|
| 174 |
+
|
| 175 |
result = (
|
| 176 |
f"### Introduction\n{introduction}\n\n"
|
| 177 |
f"### Course URL\n{course_url}\n\n"
|