Update app.py
Browse files
app.py
CHANGED
|
@@ -1,81 +1,245 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import google.generativeai as genai
|
| 4 |
import json
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
API_KEY = "AIzaSyAtm1yxPoXsz30KJUnyQNN9QeGw3FMIoMU"
|
| 8 |
genai.configure(api_key=API_KEY)
|
| 9 |
-
model = genai.GenerativeModel(model_name="gemini-2.0-flash")
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
prompt = f"""
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
Chat text:
|
| 28 |
-
\"\"\"{text}\"\"\"
|
| 29 |
"""
|
| 30 |
-
|
| 31 |
-
return response.text.strip()
|
| 32 |
-
|
| 33 |
-
# ✅ Process and return outputs
|
| 34 |
-
def process_chat(chat_text):
|
| 35 |
-
if not chat_text.strip():
|
| 36 |
-
return "", "", "", "", "", ""
|
| 37 |
-
|
| 38 |
-
analysis = analyze_text(chat_text)
|
| 39 |
-
|
| 40 |
try:
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Hugging Face's logo
|
| 2 |
+
Hugging Face
|
| 3 |
+
Models
|
| 4 |
+
Datasets
|
| 5 |
+
Spaces
|
| 6 |
+
Community
|
| 7 |
+
Docs
|
| 8 |
+
Pricing
|
| 9 |
+
|
| 10 |
+
Log In
|
| 11 |
+
Sign Up
|
| 12 |
+
Spaces:
|
| 13 |
+
|
| 14 |
+
SatyamSinghal
|
| 15 |
+
/
|
| 16 |
+
SakshiProject
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
like
|
| 20 |
+
0
|
| 21 |
+
App
|
| 22 |
+
Files
|
| 23 |
+
Community
|
| 24 |
+
SakshiProject
|
| 25 |
+
/
|
| 26 |
+
app.py
|
| 27 |
+
|
| 28 |
+
SatyamSinghal's picture
|
| 29 |
+
SatyamSinghal
|
| 30 |
+
Key updates
|
| 31 |
+
37c368e
|
| 32 |
+
verified
|
| 33 |
+
in about 10 hours
|
| 34 |
+
raw
|
| 35 |
+
|
| 36 |
+
Copy download link
|
| 37 |
+
history
|
| 38 |
+
blame
|
| 39 |
+
contribute
|
| 40 |
+
delete
|
| 41 |
+
|
| 42 |
+
7.12 kB
|
| 43 |
+
# Simple Relationship Chat Analyzer for
|
| 44 |
import gradio as gr
|
| 45 |
import google.generativeai as genai
|
| 46 |
import json
|
| 47 |
+
import re
|
| 48 |
+
from collections import Counter
|
| 49 |
|
| 50 |
+
# Configure Gemini API
|
| 51 |
+
API_KEY = "AIzaSyAtm1yxPoXsz30KJUnyQNN9QeGw3FMIoMU" # Replace with your actual API key
|
| 52 |
genai.configure(api_key=API_KEY)
|
|
|
|
| 53 |
|
| 54 |
+
model = genai.GenerativeModel(
|
| 55 |
+
model_name="gemini-2.0-flash",
|
| 56 |
+
generation_config={
|
| 57 |
+
"temperature": 0.7,
|
| 58 |
+
"max_output_tokens": 1500,
|
| 59 |
+
}
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
class SimpleAnalyzer:
|
| 63 |
+
def clean_text(self, text):
|
| 64 |
+
"""Basic text cleaning"""
|
| 65 |
+
if not text:
|
| 66 |
+
return ""
|
| 67 |
+
|
| 68 |
+
# Remove timestamps and system messages
|
| 69 |
+
text = re.sub(r'\d{1,2}:\d{2}(?::\d{2})?\s*(?:AM|PM|am|pm)?', '', text)
|
| 70 |
+
text = re.sub(r'\[.*?\]', '', text)
|
| 71 |
+
text = re.sub(r'<.*?>', '', text)
|
| 72 |
+
text = re.sub(r'\s+', ' ', text.strip())
|
| 73 |
+
|
| 74 |
+
return text
|
| 75 |
+
|
| 76 |
+
def get_basic_stats(self, text):
|
| 77 |
+
"""Get simple statistics"""
|
| 78 |
+
if not text:
|
| 79 |
+
return {}
|
| 80 |
+
|
| 81 |
+
words = text.split()
|
| 82 |
+
lines = [line.strip() for line in text.split('\n') if line.strip()]
|
| 83 |
+
|
| 84 |
+
return {
|
| 85 |
+
'word_count': len(words),
|
| 86 |
+
'message_count': len([line for line in lines if len(line) > 5]),
|
| 87 |
+
'avg_words_per_message': len(words) / max(len(lines), 1)
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
analyzer = SimpleAnalyzer()
|
| 91 |
+
|
| 92 |
+
def analyze_relationship(chat_text):
|
| 93 |
+
"""Main analysis function"""
|
| 94 |
+
if not chat_text or not chat_text.strip():
|
| 95 |
+
return "Please provide chat text to analyze.", "", "", ""
|
| 96 |
+
|
| 97 |
+
# Clean and get stats
|
| 98 |
+
cleaned_text = analyzer.clean_text(chat_text)
|
| 99 |
+
stats = analyzer.get_basic_stats(cleaned_text)
|
| 100 |
+
|
| 101 |
+
# Create analysis prompt
|
| 102 |
prompt = f"""
|
| 103 |
+
Analyze this relationship chat conversation and provide insights in this exact JSON format:
|
| 104 |
+
{{
|
| 105 |
+
"compatibility_score": number (0-100),
|
| 106 |
+
"relationship_stage": "string description",
|
| 107 |
+
"communication_style": "string description",
|
| 108 |
+
"strengths": ["strength1", "strength2", "strength3"],
|
| 109 |
+
"improvements": ["area1", "area2", "area3"],
|
| 110 |
+
"summary": "2-3 sentence relationship summary",
|
| 111 |
+
"red_flags": ["flag1", "flag2"] or []
|
| 112 |
+
}}
|
| 113 |
+
Chat text: {cleaned_text[:3000]}
|
| 114 |
+
Provide only the JSON response, no other text.
|
|
|
|
|
|
|
|
|
|
| 115 |
"""
|
| 116 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
try:
|
| 118 |
+
response = model.generate_content(prompt)
|
| 119 |
+
analysis_text = response.text.strip()
|
| 120 |
+
|
| 121 |
+
# Clean JSON response
|
| 122 |
+
if analysis_text.startswith('```json'):
|
| 123 |
+
analysis_text = analysis_text.split('```json')[1].split('```')[0].strip()
|
| 124 |
+
elif analysis_text.startswith('```'):
|
| 125 |
+
analysis_text = analysis_text.split('```')[1].split('```')[0].strip()
|
| 126 |
+
|
| 127 |
+
# Parse JSON
|
| 128 |
+
analysis = json.loads(analysis_text)
|
| 129 |
+
|
| 130 |
+
# Format outputs
|
| 131 |
+
stats_text = f"""📊 **Chat Statistics:**
|
| 132 |
+
• Words: {stats['word_count']:,}
|
| 133 |
+
• Messages: {stats['message_count']}
|
| 134 |
+
• Avg words/message: {stats['avg_words_per_message']:.1f}
|
| 135 |
+
• Relationship Stage: {analysis.get('relationship_stage', 'Unknown')}"""
|
| 136 |
+
|
| 137 |
+
compatibility_text = f"**Compatibility Score: {analysis.get('compatibility_score', 0)}/100**"
|
| 138 |
+
|
| 139 |
+
strengths_text = "**💪 Strengths:**\n" + "\n".join([f"• {s}" for s in analysis.get('strengths', [])])
|
| 140 |
+
|
| 141 |
+
improvements_text = "**🎯 Areas to Improve:**\n" + "\n".join([f"• {i}" for i in analysis.get('improvements', [])])
|
| 142 |
+
|
| 143 |
+
summary_text = f"**📖 Summary:**\n{analysis.get('summary', 'No summary available')}"
|
| 144 |
+
|
| 145 |
+
red_flags = analysis.get('red_flags', [])
|
| 146 |
+
if red_flags:
|
| 147 |
+
red_flags_text = "**⚠️ Red Flags:**\n" + "\n".join([f"• {flag}" for flag in red_flags])
|
| 148 |
+
else:
|
| 149 |
+
red_flags_text = "**✅ No significant red flags detected**"
|
| 150 |
+
|
| 151 |
+
full_analysis = f"""{summary_text}
|
| 152 |
+
{compatibility_text}
|
| 153 |
+
{strengths_text}
|
| 154 |
+
{improvements_text}
|
| 155 |
+
{red_flags_text}
|
| 156 |
+
**🗣️ Communication Style:** {analysis.get('communication_style', 'Not analyzed')}"""
|
| 157 |
+
|
| 158 |
+
return full_analysis, stats_text, compatibility_text, f"{analysis.get('compatibility_score', 0)}"
|
| 159 |
+
|
| 160 |
+
except json.JSONDecodeError:
|
| 161 |
+
return f"AI Response (couldn't parse as JSON):\n{analysis_text}", stats_text, "Score not available", "0"
|
| 162 |
+
except Exception as e:
|
| 163 |
+
return f"Error: {str(e)}", "Stats unavailable", "Score unavailable", "0"
|
| 164 |
+
|
| 165 |
+
# Create Gradio interface
|
| 166 |
+
def create_app():
|
| 167 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="💕 Simple Chat Analyzer") as app:
|
| 168 |
+
|
| 169 |
+
gr.HTML("""
|
| 170 |
+
<div style="text-align: center; margin-bottom: 30px;">
|
| 171 |
+
<h1>💕 Simple Relationship Chat Analyzer</h1>
|
| 172 |
+
<p><em>Quick insights into your relationship through chat analysis</em></p>
|
| 173 |
+
</div>
|
| 174 |
+
""")
|
| 175 |
+
|
| 176 |
+
with gr.Row():
|
| 177 |
+
with gr.Column(scale=2):
|
| 178 |
+
chat_input = gr.Textbox(
|
| 179 |
+
label="📝 Paste Your Chat Conversation",
|
| 180 |
+
placeholder="Paste your WhatsApp, text messages, or any chat conversation here...",
|
| 181 |
+
lines=12,
|
| 182 |
+
max_lines=20
|
| 183 |
+
)
|
| 184 |
+
|
| 185 |
+
with gr.Row():
|
| 186 |
+
analyze_btn = gr.Button("🔍 Analyze", variant="primary", scale=2)
|
| 187 |
+
clear_btn = gr.Button("🗑️ Clear", variant="secondary", scale=1)
|
| 188 |
+
|
| 189 |
+
with gr.Column(scale=1):
|
| 190 |
+
stats_output = gr.Textbox(
|
| 191 |
+
label="📊 Quick Stats",
|
| 192 |
+
interactive=False,
|
| 193 |
+
lines=8
|
| 194 |
+
)
|
| 195 |
+
|
| 196 |
+
score_output = gr.Textbox(
|
| 197 |
+
label="💯 Compatibility",
|
| 198 |
+
interactive=False,
|
| 199 |
+
lines=2
|
| 200 |
+
)
|
| 201 |
+
|
| 202 |
+
# Main results
|
| 203 |
+
analysis_output = gr.Textbox(
|
| 204 |
+
label="🎯 Relationship Analysis",
|
| 205 |
+
interactive=False,
|
| 206 |
+
lines=15,
|
| 207 |
+
max_lines=25
|
| 208 |
+
)
|
| 209 |
+
|
| 210 |
+
# Event handlers
|
| 211 |
+
def analyze_handler(text):
|
| 212 |
+
return analyze_relationship(text)
|
| 213 |
+
|
| 214 |
+
def clear_handler():
|
| 215 |
+
return "", "", "", "", ""
|
| 216 |
+
|
| 217 |
+
analyze_btn.click(
|
| 218 |
+
fn=analyze_handler,
|
| 219 |
+
inputs=[chat_input],
|
| 220 |
+
outputs=[analysis_output, stats_output, score_output, gr.State()]
|
| 221 |
+
)
|
| 222 |
+
|
| 223 |
+
clear_btn.click(
|
| 224 |
+
fn=clear_handler,
|
| 225 |
+
outputs=[chat_input, analysis_output, stats_output, score_output, gr.State()]
|
| 226 |
+
)
|
| 227 |
+
|
| 228 |
+
gr.HTML("""
|
| 229 |
+
<div style="text-align: center; margin-top: 20px; padding: 15px; background: #f0f0f0; border-radius: 8px;">
|
| 230 |
+
<p><strong>💡 Tips:</strong> Include longer conversations for better analysis • Remove personal info • Results are for guidance only</p>
|
| 231 |
+
</div>
|
| 232 |
+
""")
|
| 233 |
+
|
| 234 |
+
return app
|
| 235 |
+
|
| 236 |
+
# Launch the app
|
| 237 |
+
if __name__ == "__main__":
|
| 238 |
+
print("🚀 Starting Simple Relationship Chat Analyzer...")
|
| 239 |
+
|
| 240 |
+
app = create_app()
|
| 241 |
+
app.launch(
|
| 242 |
+
share=True,
|
| 243 |
+
server_name="0.0.0.0",
|
| 244 |
+
server_port=7860
|
| 245 |
+
)
|