Script-Analyzer / app.py
airpm21's picture
Create app.py
379f35a verified
import gradio as gr
import spacy
from transformers import pipeline
import json
# Load models once at startup
nlp = spacy.load("en_core_web_sm")
classifier = pipeline("zero-shot-classification")
def analyze_sentence_purposes(script):
purposes = []
doc = nlp(script)
for sent in doc.sents:
if any(token.text in ['?', 'how', 'why', 'what'] for token in sent):
purposes.append(("QUESTION", "Engages curiosity"))
elif any(token.lemma_ in ['show', 'teach', 'explain'] for token in sent):
purposes.append(("INSTRUCTION", "Provides guidance"))
elif any(token.lemma_ in ['discover', 'find', 'reveal'] for token in sent):
purposes.append(("REVELATION", "Shares discovery"))
elif any(token.lemma_ in ['struggle', 'problem', 'difficult'] for token in sent):
purposes.append(("CONFLICT", "Presents challenge"))
elif any(token.lemma_ in ['result', 'success', 'transform'] for token in sent):
purposes.append(("RESOLUTION", "Shows payoff"))
else:
purposes.append(("CONTEXT", "Sets scene"))
return purposes
def map_emotional_arc(script):
emotional_phases = []
doc = nlp(script)
emotional_indicators = {
'struggle': ['frustrat', 'difficult', 'struggle', 'problem', 'failed'],
'curiosity': ['wonder', 'curious', 'question', 'mystery', 'secret'],
'discovery': ['find', 'discover', 'learn', 'realize', 'figure out'],
'transformation': ['change', 'transform', 'better', 'improve', 'success'],
'satisfaction': ['happy', 'proud', 'excited', 'amazing', 'awesome']
}
for sent in doc.sents:
sentence_emotion = "NEUTRAL"
max_strength = 0
for emotion, keywords in emotional_indicators.items():
strength = sum(1 for token in sent if token.lemma_ in keywords)
if strength > max_strength:
max_strength = strength
sentence_emotion = emotion.upper()
emotional_phases.append(sentence_emotion)
return emotional_phases
def detect_narrative_pattern(semantic_analysis):
purposes = [p[0] for p in semantic_analysis['purposes']]
emotions = semantic_analysis['emotional_arc']
pattern_signatures = {
'TRANSFORMATION': [
['CONFLICT', 'REVELATION', 'RESOLUTION'],
['struggle', 'discovery', 'transformation']
],
'INVESTIGATION': [
['QUESTION', 'REVELATION', 'RESOLUTION'],
['curiosity', 'discovery', 'satisfaction']
],
'STORYTELLING': [
['CONTEXT', 'CONFLICT', 'RESOLUTION'],
['neutral', 'struggle', 'satisfaction']
],
'EXPLANATION': [
['QUESTION', 'INSTRUCTION', 'RESOLUTION'],
['curiosity', 'neutral', 'satisfaction']
]
}
best_pattern = None
highest_match = 0
for pattern_name, signature in pattern_signatures.items():
purpose_match = sum(1 for i, purpose in enumerate(purposes)
if i < len(signature[0]) and purpose == signature[0][i])
emotion_match = sum(1 for i, emotion in enumerate(emotions)
if i < len(signature[1]) and emotion.lower() == signature[1][i])
total_match = purpose_match + emotion_match
if total_match > highest_match:
highest_match = total_match
best_pattern = pattern_name
return best_pattern, highest_match / (len(purposes) + len(emotions))
def analyze_creator_intent(script, semantic_analysis):
doc = nlp(script)
intent_indicators = {
'EDUCATE': ['teach', 'explain', 'show', 'learn', 'understand'],
'ENTERTAIN': ['funny', 'hilarious', 'entertain', 'enjoy', 'laugh'],
'INSPIRE': ['motivate', 'inspire', 'empower', 'transform', 'change'],
'PERSUADE': ['should', 'must', 'need to', 'recommend', 'best'],
'DOCUMENT': ['experience', 'journey', 'story', 'happened', 'went']
}
intent_scores = {intent: 0 for intent in intent_indicators.keys()}
for token in doc:
for intent, keywords in intent_indicators.items():
if token.lemma_ in keywords:
intent_scores[intent] += 1
if any(p[0] == 'INSTRUCTION' for p in semantic_analysis['purposes']):
intent_scores['EDUCATE'] += 2
if 'struggle' in semantic_analysis['emotional_arc']:
intent_scores['INSPIRE'] += 2
primary_intent = max(intent_scores.items(), key=lambda x: x[1])
return primary_intent[0], intent_scores
def detect_contextual_domain(script):
doc = nlp(script)
domain_indicators = {
'TECHNOLOGY': ['software', 'app', 'code', 'digital', 'algorithm'],
'HEALTH': ['energy', 'health', 'body', 'mind', 'wellness'],
'BUSINESS': ['money', 'business', 'market', 'profit', 'investment'],
'CREATIVE': ['design', 'create', 'art', 'build', 'make'],
'ACADEMIC': ['research', 'study', 'theory', 'data', 'analysis']
}
domain_scores = {domain: 0 for domain in domain_indicators.keys()}
for token in doc:
for domain, terms in domain_indicators.items():
if token.lemma_ in terms:
domain_scores[domain] += 1
primary_domain = max(domain_scores.items(), key=lambda x: x[1])
sentence_complexity = sum(len(list(sent.root.children)) for sent in doc.sents) / len(list(doc.sents))
sophistication = "ADVANCED" if sentence_complexity > 5 else "INTERMEDIATE" if sentence_complexity > 3 else "BEGINNER"
return primary_domain[0], sophistication, domain_scores
def analyze_script(script):
"""NLP-First Universal Script Analysis"""
if not script.strip():
return "❌ Please enter a script to analyze"
try:
# Layer 1: Semantic Understanding
semantic_results = {
'purposes': analyze_sentence_purposes(script),
'emotional_arc': map_emotional_arc(script)
}
# Layer 2: Narrative Pattern Detection
pattern, pattern_confidence = detect_narrative_pattern(semantic_results)
# Layer 3: Intent & Domain Intelligence
primary_intent, intent_scores = analyze_creator_intent(script, semantic_results)
primary_domain, sophistication, domain_scores = detect_contextual_domain(script)
# Format results
result = f"""
🎯 **UNIVERSAL SCRIPT ANALYSIS**
🧠 **SEMANTIC UNDERSTANDING:**
- Sentence Purposes: {', '.join([p[0] for p in semantic_results['purposes']])}
- Emotional Arc: {', '.join(semantic_results['emotional_arc'])}
🎭 **NARRATIVE PATTERN:**
- Primary Pattern: {pattern} ({pattern_confidence:.1%} confidence)
🎯 **CREATOR INTENT:**
- Primary Intent: {primary_intent}
- Intent Breakdown: {', '.join([f'{k}:{v}' for k,v in intent_scores.items() if v > 0])}
🌍 **DOMAIN INTELLIGENCE:**
- Primary Domain: {primary_domain}
- Sophistication: {sophistication}
- Domain Scores: {', '.join([f'{k}:{v}' for k,v in domain_scores.items() if v > 0])}
πŸ“Š **CONTENT BLUEPRINT:** {pattern} + {primary_domain} Domain
"""
return result
except Exception as e:
return f"❌ Error analyzing script: {str(e)}"
# Create Gradio interface
demo = gr.Interface(
fn=analyze_script,
inputs=gr.Textbox(lines=10, placeholder="Paste your script here...", label="Script"),
outputs=gr.Textbox(label="Analysis Results"),
title="πŸ€– Universal Script Analyzer Pro",
description="NLP-First Analysis: Semantic Understanding β†’ Narrative Patterns β†’ Intent & Domain"
)
# For API access
def api_analyze(script):
return analyze_script(script)
if __name__ == "__main__":
demo.launch(share=True)