Spaces:
Sleeping
Sleeping
Chore: Added console logs for debugging.
Browse files
app.py
CHANGED
|
@@ -1,24 +1,18 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
import json
|
| 3 |
-
|
| 4 |
import numpy as np
|
| 5 |
-
from
|
| 6 |
|
| 7 |
-
import
|
| 8 |
-
import warnings
|
| 9 |
-
# from fastapi import FastAPI, HTTPException
|
| 10 |
-
# from pydantic import BaseModel
|
| 11 |
import gradio as gr
|
| 12 |
-
|
| 13 |
|
| 14 |
# Suppress warnings
|
| 15 |
os.environ['TRANSFORMERS_VERBOSITY'] = 'error'
|
| 16 |
os.environ['TOKENIZERS_PARALLELISM'] = 'false'
|
| 17 |
warnings.filterwarnings("ignore")
|
| 18 |
|
| 19 |
-
# Initialize FastAPI
|
| 20 |
-
# app = FastAPI(title="SelahSearch NLP Agent")
|
| 21 |
-
|
| 22 |
# Load Model
|
| 23 |
MODEL_NAME = "odunola/sentence-transformers-bible-reference-final"
|
| 24 |
model = SentenceTransformer(MODEL_NAME)
|
|
@@ -31,6 +25,7 @@ THEMES = ["Trust and Guidance", "Restoration and Peace", "Wrath and Judgment", "
|
|
| 31 |
THEME_VECS = model.encode(THEMES, convert_to_numpy=True)
|
| 32 |
THEME_VECS = THEME_VECS / np.linalg.norm(THEME_VECS, axis=1, keepdims=True)
|
| 33 |
|
|
|
|
| 34 |
def chunk_text(text):
|
| 35 |
words = text.split()
|
| 36 |
return [" ".join(words[i : i + 400]) for i in range(0, len(words), 200)]
|
|
@@ -47,15 +42,6 @@ def get_thematic_signature(doc_vec):
|
|
| 47 |
norm = np.linalg.norm(relu_scores)
|
| 48 |
return relu_scores / (norm if norm > 0 else 1.0)
|
| 49 |
|
| 50 |
-
# # Define Data Models
|
| 51 |
-
# class Song(BaseModel):
|
| 52 |
-
# name: str
|
| 53 |
-
# lyrics: str
|
| 54 |
-
|
| 55 |
-
# class AnalysisRequest(BaseModel):
|
| 56 |
-
# passage: str
|
| 57 |
-
# songs: List[Song]
|
| 58 |
-
|
| 59 |
|
| 60 |
def analyze_thematic_similarity(passage_text, songs_json):
|
| 61 |
"""
|
|
@@ -97,6 +83,7 @@ def analyze_thematic_similarity(passage_text, songs_json):
|
|
| 97 |
# Sort by score descending
|
| 98 |
results.sort(key=lambda x: x['score'], reverse=True)
|
| 99 |
return results
|
|
|
|
| 100 |
except Exception as e:
|
| 101 |
raise gr.Error(f"NLP Worker Error: {str(e)}")
|
| 102 |
|
|
@@ -114,51 +101,3 @@ demo = gr.Interface(
|
|
| 114 |
|
| 115 |
if __name__ == "__main__":
|
| 116 |
demo.queue().launch() # demo.queue() is vital for high RAM reliability
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
# @app.get("/healthcheck")
|
| 120 |
-
# def read_root():
|
| 121 |
-
# return {"Status": "Alive"}
|
| 122 |
-
|
| 123 |
-
# @app.post("/analyse")
|
| 124 |
-
# async def analyze_similarity(data: AnalysisRequest):
|
| 125 |
-
# try:
|
| 126 |
-
# p_vec = get_normalized_vector(data.passage)
|
| 127 |
-
# p_sig = get_thematic_signature(p_vec)
|
| 128 |
-
|
| 129 |
-
# results = []
|
| 130 |
-
|
| 131 |
-
# for song in data.songs:
|
| 132 |
-
# # Process Song Lyrics
|
| 133 |
-
# l_vec = get_normalized_vector(song.lyrics)
|
| 134 |
-
# direct_sim = float(np.dot(p_vec, l_vec))
|
| 135 |
-
|
| 136 |
-
# relevant_themes = []
|
| 137 |
-
# final_score = direct_sim
|
| 138 |
-
|
| 139 |
-
# # Original Threshold Logic
|
| 140 |
-
# if direct_sim >= 0.1:
|
| 141 |
-
# l_sig = get_thematic_signature(l_vec)
|
| 142 |
-
# thematic_sim = float(np.dot(p_sig, l_sig))
|
| 143 |
-
|
| 144 |
-
# contributions = p_sig * l_sig
|
| 145 |
-
# relevant_themes = [THEMES[i] for i, val in enumerate(contributions) if val > 0.05]
|
| 146 |
-
|
| 147 |
-
# # 60/40 weighted split
|
| 148 |
-
# final_score = (0.6 * direct_sim) + (0.4 * thematic_sim)
|
| 149 |
-
|
| 150 |
-
# results.append({
|
| 151 |
-
# "name": song.name,
|
| 152 |
-
# "score": round(final_score, 4),
|
| 153 |
-
# "themes": relevant_themes
|
| 154 |
-
# })
|
| 155 |
-
|
| 156 |
-
# # Sort by score descending
|
| 157 |
-
# results.sort(key=lambda x: x['score'], reverse=True)
|
| 158 |
-
# return results
|
| 159 |
-
# except Exception as e:
|
| 160 |
-
# raise HTTPException(status_code=500, detail=str(e))
|
| 161 |
-
|
| 162 |
-
# if __name__ == "__main__":
|
| 163 |
-
# import uvicorn
|
| 164 |
-
# uvicorn.run(app, host="0.0.0.0", port=int(os.getenv("PORT", 8000)))
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import warnings
|
| 3 |
import json
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
+
from typing import List
|
| 6 |
|
| 7 |
+
from sentence_transformers import SentenceTransformer
|
|
|
|
|
|
|
|
|
|
| 8 |
import gradio as gr
|
| 9 |
+
|
| 10 |
|
| 11 |
# Suppress warnings
|
| 12 |
os.environ['TRANSFORMERS_VERBOSITY'] = 'error'
|
| 13 |
os.environ['TOKENIZERS_PARALLELISM'] = 'false'
|
| 14 |
warnings.filterwarnings("ignore")
|
| 15 |
|
|
|
|
|
|
|
|
|
|
| 16 |
# Load Model
|
| 17 |
MODEL_NAME = "odunola/sentence-transformers-bible-reference-final"
|
| 18 |
model = SentenceTransformer(MODEL_NAME)
|
|
|
|
| 25 |
THEME_VECS = model.encode(THEMES, convert_to_numpy=True)
|
| 26 |
THEME_VECS = THEME_VECS / np.linalg.norm(THEME_VECS, axis=1, keepdims=True)
|
| 27 |
|
| 28 |
+
|
| 29 |
def chunk_text(text):
|
| 30 |
words = text.split()
|
| 31 |
return [" ".join(words[i : i + 400]) for i in range(0, len(words), 200)]
|
|
|
|
| 42 |
norm = np.linalg.norm(relu_scores)
|
| 43 |
return relu_scores / (norm if norm > 0 else 1.0)
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
def analyze_thematic_similarity(passage_text, songs_json):
|
| 47 |
"""
|
|
|
|
| 83 |
# Sort by score descending
|
| 84 |
results.sort(key=lambda x: x['score'], reverse=True)
|
| 85 |
return results
|
| 86 |
+
|
| 87 |
except Exception as e:
|
| 88 |
raise gr.Error(f"NLP Worker Error: {str(e)}")
|
| 89 |
|
|
|
|
| 101 |
|
| 102 |
if __name__ == "__main__":
|
| 103 |
demo.queue().launch() # demo.queue() is vital for high RAM reliability
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|