abhidgp1978 commited on
Commit
bb6e7a7
·
verified ·
1 Parent(s): a36375c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -27
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 truncate_text_by_words(text, max_words):
103
- words = text.split()
104
- if len(words) <= max_words:
105
- return text
106
- truncated = " ".join(words[:max_words])
107
- return truncated
108
-
109
-
110
- def extract_course_intro(text, max_words=750):
111
- # Split text into paragraphs, keep only first 3-5 paragraphs until max_words
112
- paragraphs = [p.strip() for p in text.split('\n') if p.strip()]
113
- selected_paragraphs = []
114
- total_words = 0
115
- for p in paragraphs:
116
- p_words = len(p.split())
117
- if total_words + p_words <= max_words:
118
- selected_paragraphs.append(p)
119
- total_words += p_words
 
120
  else:
121
- remaining = max_words - total_words
122
- if remaining > 0:
123
- selected_paragraphs.append(truncate_text_by_words(p, remaining))
124
- break
125
- if len(selected_paragraphs) >= 5:
126
- break
127
- return "\n\n".join(selected_paragraphs)
 
 
 
 
 
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 = extract_course_intro(course_text, max_words=750)
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"