Update app.py
Browse files
app.py
CHANGED
@@ -72,28 +72,42 @@ def summarize_text(text: str):
|
|
72 |
output = summarizer(prompt, max_length=256, do_sample=False)
|
73 |
return output[0]["generated_text"].strip()
|
74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
async def update_knowledge_graph_periodically():
|
76 |
-
queries = [
|
77 |
-
"latest tech startup news",
|
78 |
-
"AI breakthroughs 2025",
|
79 |
-
"funding trends in tech startups",
|
80 |
-
"popular programming languages 2025",
|
81 |
-
"open source AI models"
|
82 |
-
]
|
83 |
while True:
|
84 |
-
query = random.choice(queries)
|
85 |
-
print(f"[KG Updater] Searching DuckDuckGo for query: {query}")
|
86 |
try:
|
|
|
|
|
|
|
87 |
ddg_data = await fetch_ddg_search(query)
|
88 |
cleaned = clean_ddg_text(ddg_data)
|
89 |
-
if not cleaned:
|
90 |
-
cleaned = "No useful info found."
|
91 |
-
print(f"[KG Updater] DuckDuckGo cleaned text length: {len(cleaned)}")
|
92 |
|
93 |
-
|
|
|
|
|
|
|
|
|
94 |
loop = asyncio.get_event_loop()
|
95 |
-
summary = await loop.run_in_executor(
|
96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
|
98 |
knowledge_graph[query] = {
|
99 |
"raw_text": cleaned,
|
@@ -105,7 +119,8 @@ async def update_knowledge_graph_periodically():
|
|
105 |
except Exception as e:
|
106 |
print(f"[KG Updater] Error: {e}")
|
107 |
|
108 |
-
await asyncio.sleep(60)
|
|
|
109 |
|
110 |
@app.on_event("startup")
|
111 |
async def startup_event():
|
|
|
72 |
output = summarizer(prompt, max_length=256, do_sample=False)
|
73 |
return output[0]["generated_text"].strip()
|
74 |
|
75 |
+
async def generate_random_query():
|
76 |
+
"""Use your main model to invent a search query."""
|
77 |
+
prompt = (
|
78 |
+
"Generate one short, interesting, real-world question "
|
79 |
+
"about technology, startups, AI, or science that someone might Google. "
|
80 |
+
"Output only the query, nothing else."
|
81 |
+
)
|
82 |
+
output = generator(prompt, max_new_tokens=20, do_sample=True, temperature=0.9, truncation=True)
|
83 |
+
query = output[0]["generated_text"].strip()
|
84 |
+
# Safety: strip weird trailing punctuation
|
85 |
+
return query.split("\n")[0].strip("?!. ")
|
86 |
+
|
87 |
async def update_knowledge_graph_periodically():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
while True:
|
|
|
|
|
89 |
try:
|
90 |
+
query = await generate_random_query()
|
91 |
+
print(f"[KG Updater] Searching DuckDuckGo for query: {query}")
|
92 |
+
|
93 |
ddg_data = await fetch_ddg_search(query)
|
94 |
cleaned = clean_ddg_text(ddg_data)
|
|
|
|
|
|
|
95 |
|
96 |
+
if not cleaned or len(cleaned) < 50:
|
97 |
+
print("[KG Updater] Too little info found, retrying with new query...")
|
98 |
+
continue # Skip saving, go to next loop
|
99 |
+
|
100 |
+
# Summarize with instruct model
|
101 |
loop = asyncio.get_event_loop()
|
102 |
+
summary = await loop.run_in_executor(
|
103 |
+
None,
|
104 |
+
lambda: summarizer(
|
105 |
+
f"Summarize this info concisely:\n{cleaned}",
|
106 |
+
max_length=256,
|
107 |
+
truncation=True,
|
108 |
+
do_sample=False
|
109 |
+
)[0]["generated_text"].strip()
|
110 |
+
)
|
111 |
|
112 |
knowledge_graph[query] = {
|
113 |
"raw_text": cleaned,
|
|
|
119 |
except Exception as e:
|
120 |
print(f"[KG Updater] Error: {e}")
|
121 |
|
122 |
+
await asyncio.sleep(60) # Run forever, every 60s
|
123 |
+
|
124 |
|
125 |
@app.on_event("startup")
|
126 |
async def startup_event():
|