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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import tempfile
|
| 4 |
+
from typing import List, Dict, Any
|
| 5 |
+
import fitz # PyMuPDF for PDF processing
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import pytesseract
|
| 8 |
+
import io
|
| 9 |
+
import json
|
| 10 |
+
from datetime import datetime
|
| 11 |
+
|
| 12 |
+
# Set up Tesseract OCR (make sure it's installed on your system)
|
| 13 |
+
pytesseract.pytesseract.tesseract_cmd = '/usr/bin/tesseract' # Update this path as needed
|
| 14 |
+
|
| 15 |
+
# Custom theme for a warm, loving interface
|
| 16 |
+
custom_theme = gr.themes.Soft(
|
| 17 |
+
primary_hue="pink",
|
| 18 |
+
secondary_hue="red",
|
| 19 |
+
neutral_hue="slate",
|
| 20 |
+
font=gr.themes.GoogleFont("Inter"),
|
| 21 |
+
text_size="lg",
|
| 22 |
+
spacing_size="lg",
|
| 23 |
+
radius_size="lg"
|
| 24 |
+
).set(
|
| 25 |
+
button_primary_background_fill="*primary_600",
|
| 26 |
+
button_primary_background_fill_hover="*primary_700",
|
| 27 |
+
block_title_text_weight="600",
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
def extract_text_from_pdf(pdf_path: str) -> str:
|
| 31 |
+
"""Extract text from PDF file using PyMuPDF"""
|
| 32 |
+
try:
|
| 33 |
+
doc = fitz.open(pdf_path)
|
| 34 |
+
text = ""
|
| 35 |
+
for page in doc:
|
| 36 |
+
text += page.get_text()
|
| 37 |
+
return text
|
| 38 |
+
except Exception as e:
|
| 39 |
+
raise gr.Error(f"Error processing PDF: {str(e)}")
|
| 40 |
+
|
| 41 |
+
def extract_text_from_image(image_path: str) -> str:
|
| 42 |
+
"""Extract text from image using Tesseract OCR"""
|
| 43 |
+
try:
|
| 44 |
+
img = Image.open(image_path)
|
| 45 |
+
text = pytesseract.image_to_string(img)
|
| 46 |
+
return text
|
| 47 |
+
except Exception as e:
|
| 48 |
+
raise gr.Error(f"Error processing image: {str(e)}")
|
| 49 |
+
|
| 50 |
+
def extract_text_from_txt(txt_path: str) -> str:
|
| 51 |
+
"""Extract text from TXT file"""
|
| 52 |
+
try:
|
| 53 |
+
with open(txt_path, 'r', encoding='utf-8') as f:
|
| 54 |
+
return f.read()
|
| 55 |
+
except Exception as e:
|
| 56 |
+
raise gr.Error(f"Error reading text file: {str(e)}")
|
| 57 |
+
|
| 58 |
+
def process_uploaded_files(files: List[Dict[str, Any]]) -> str:
|
| 59 |
+
"""Process all uploaded files and extract text content"""
|
| 60 |
+
all_text = ""
|
| 61 |
+
|
| 62 |
+
for file_data in files:
|
| 63 |
+
file_path = file_data['name']
|
| 64 |
+
file_ext = os.path.splitext(file_path)[1].lower()
|
| 65 |
+
|
| 66 |
+
if file_ext == '.pdf':
|
| 67 |
+
text = extract_text_from_pdf(file_path)
|
| 68 |
+
elif file_ext in ['.png', '.jpg', '.jpeg', '.gif', '.bmp']:
|
| 69 |
+
text = extract_text_from_image(file_path)
|
| 70 |
+
elif file_ext == '.txt':
|
| 71 |
+
text = extract_text_from_txt(file_path)
|
| 72 |
+
else:
|
| 73 |
+
raise gr.Error(f"Unsupported file type: {file_ext}")
|
| 74 |
+
|
| 75 |
+
all_text += f"\n\n=== Content from {os.path.basename(file_path)} ===\n\n"
|
| 76 |
+
all_text += text
|
| 77 |
+
|
| 78 |
+
return all_text
|
| 79 |
+
|
| 80 |
+
def analyze_relationship(person_name: str, relationship_history: str) -> Dict[str, Any]:
|
| 81 |
+
"""
|
| 82 |
+
Analyze relationship history and generate a love guide.
|
| 83 |
+
This is a mock function - in a real app, you would use an AI model here.
|
| 84 |
+
"""
|
| 85 |
+
if not person_name.strip():
|
| 86 |
+
raise gr.Error("Please enter the person's name")
|
| 87 |
+
|
| 88 |
+
if not relationship_history.strip():
|
| 89 |
+
raise gr.Error("Please upload at least one document")
|
| 90 |
+
|
| 91 |
+
# Mock analysis - replace with actual AI model calls
|
| 92 |
+
analysis = {
|
| 93 |
+
"person_name": person_name,
|
| 94 |
+
"analysis_date": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
| 95 |
+
"key_traits": [
|
| 96 |
+
"Loyal",
|
| 97 |
+
"Affectionate",
|
| 98 |
+
"Good listener",
|
| 99 |
+
"Supportive",
|
| 100 |
+
"Adventurous"
|
| 101 |
+
],
|
| 102 |
+
"love_language": "Quality Time",
|
| 103 |
+
"communication_style": "Open and honest",
|
| 104 |
+
"conflict_resolution": "Calm discussion",
|
| 105 |
+
"ideal_partner_traits": [
|
| 106 |
+
"Patient",
|
| 107 |
+
"Understanding",
|
| 108 |
+
"Communicative",
|
| 109 |
+
"Supportive",
|
| 110 |
+
"Trustworthy"
|
| 111 |
+
],
|
| 112 |
+
"relationship_goals": [
|
| 113 |
+
"Build trust",
|
| 114 |
+
"Improve communication",
|
| 115 |
+
"Create shared experiences",
|
| 116 |
+
"Support each other's growth"
|
| 117 |
+
],
|
| 118 |
+
"love_guide": {
|
| 119 |
+
"daily_affirmations": [
|
| 120 |
+
f"Tell {person_name} how much you appreciate them",
|
| 121 |
+
"Give genuine compliments",
|
| 122 |
+
"Show interest in their day"
|
| 123 |
+
],
|
| 124 |
+
"quality_time": [
|
| 125 |
+
"Plan regular date nights",
|
| 126 |
+
"Have deep conversations",
|
| 127 |
+
"Create shared hobbies"
|
| 128 |
+
],
|
| 129 |
+
"conflict_tips": [
|
| 130 |
+
"Stay calm and listen",
|
| 131 |
+
"Use 'I' statements",
|
| 132 |
+
"Focus on solutions"
|
| 133 |
+
]
|
| 134 |
+
}
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
return analysis
|
| 138 |
+
|
| 139 |
+
def generate_love_guide(person_name: str, files: List[Dict[str, Any]]) -> Dict[str, Any]:
|
| 140 |
+
"""Main function to process files and generate love guide"""
|
| 141 |
+
try:
|
| 142 |
+
# Process uploaded files
|
| 143 |
+
relationship_history = process_uploaded_files(files)
|
| 144 |
+
|
| 145 |
+
# Analyze relationship
|
| 146 |
+
analysis = analyze_relationship(person_name, relationship_history)
|
| 147 |
+
|
| 148 |
+
return analysis
|
| 149 |
+
except Exception as e:
|
| 150 |
+
raise gr.Error(f"Error generating love guide: {str(e)}")
|
| 151 |
+
|
| 152 |
+
def format_analysis(analysis: Dict[str, Any]) -> str:
|
| 153 |
+
"""Format analysis results for display"""
|
| 154 |
+
if not analysis:
|
| 155 |
+
return "No analysis available"
|
| 156 |
+
|
| 157 |
+
formatted = f"""
|
| 158 |
+
# π Love Guide for {analysis['person_name']}
|
| 159 |
+
|
| 160 |
+
**Analysis Date:** {analysis['analysis_date']}
|
| 161 |
+
|
| 162 |
+
## π― Key Traits
|
| 163 |
+
{' β’ '.join(analysis['key_traits'])}
|
| 164 |
+
|
| 165 |
+
## π¬ Love Language
|
| 166 |
+
**{analysis['love_language']}** - This person values meaningful time together and undivided attention.
|
| 167 |
+
|
| 168 |
+
## π£οΈ Communication Style
|
| 169 |
+
**{analysis['communication_style']}** - They appreciate open, honest conversations.
|
| 170 |
+
|
| 171 |
+
## β¨ Ideal Partner Traits
|
| 172 |
+
{' β’ '.join(analysis['ideal_partner_traits'])}
|
| 173 |
+
|
| 174 |
+
## π― Relationship Goals
|
| 175 |
+
{' β’ '.join(analysis['relationship_goals'])}
|
| 176 |
+
|
| 177 |
+
## π‘ Love Guide
|
| 178 |
+
|
| 179 |
+
### Daily Affirmations
|
| 180 |
+
{' β’ '.join(analysis['love_guide']['daily_affirmations'])}
|
| 181 |
+
|
| 182 |
+
### Quality Time Ideas
|
| 183 |
+
{' β’ '.join(analysis['love_guide']['quality_time'])}
|
| 184 |
+
|
| 185 |
+
### Conflict Resolution Tips
|
| 186 |
+
{' β’ '.join(analysis['love_guide']['conflict_tips'])}
|
| 187 |
+
|
| 188 |
+
---
|
| 189 |
+
|
| 190 |
+
**Remember:** Every relationship is unique. Use this guide as inspiration and adapt it to your specific situation.
|
| 191 |
+
"""
|
| 192 |
+
|
| 193 |
+
return formatted
|
| 194 |
+
|
| 195 |
+
def save_analysis(analysis: Dict[str, Any]) -> str:
|
| 196 |
+
"""Save analysis to JSON file"""
|
| 197 |
+
try:
|
| 198 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 199 |
+
filename = f"love_guide_{analysis['person_name']}_{timestamp}.json"
|
| 200 |
+
|
| 201 |
+
# Save to temporary file
|
| 202 |
+
temp_dir = tempfile.gettempdir()
|
| 203 |
+
file_path = os.path.join(temp_dir, filename)
|
| 204 |
+
|
| 205 |
+
with open(file_path, 'w', encoding='utf-8') as f:
|
| 206 |
+
json.dump(analysis, f, indent=2, ensure_ascii=False)
|
| 207 |
+
|
| 208 |
+
return file_path
|
| 209 |
+
except Exception as e:
|
| 210 |
+
raise gr.Error(f"Error saving analysis: {str(e)}")
|
| 211 |
+
|
| 212 |
+
# Create Gradio interface
|
| 213 |
+
with gr.Blocks() as demo:
|
| 214 |
+
gr.Markdown("""
|
| 215 |
+
# π Love Guide Generator
|
| 216 |
+
|
| 217 |
+
**Built with anycoder** - [Visit our Space](https://huggingface.co/spaces/akhaliq/anycoder)
|
| 218 |
+
|
| 219 |
+
Upload documents from your relationship history (texts, PDFs, or images) and let AI analyze the patterns to create a personalized guide to loving your partner and understanding their ideal relationship.
|
| 220 |
+
""")
|
| 221 |
+
|
| 222 |
+
with gr.Row():
|
| 223 |
+
with gr.Column(scale=1):
|
| 224 |
+
gr.Markdown("## π Upload Relationship Documents")
|
| 225 |
+
gr.Markdown("Upload transcripts, messages, or any documents that show your relationship history.")
|
| 226 |
+
|
| 227 |
+
file_upload = gr.File(
|
| 228 |
+
label="Upload Documents",
|
| 229 |
+
file_types=["text", "pdf", "image"],
|
| 230 |
+
file_count="multiple",
|
| 231 |
+
type="filepath",
|
| 232 |
+
height=150
|
| 233 |
+
)
|
| 234 |
+
|
| 235 |
+
person_name = gr.Textbox(
|
| 236 |
+
label="Person's Name",
|
| 237 |
+
placeholder="Enter the name of the person to analyze",
|
| 238 |
+
lines=1
|
| 239 |
+
)
|
| 240 |
+
|
| 241 |
+
analyze_btn = gr.Button("π Generate Love Guide", variant="primary", size="lg")
|
| 242 |
+
|
| 243 |
+
with gr.Column(scale=1):
|
| 244 |
+
gr.Markdown("## π Analysis Results")
|
| 245 |
+
|
| 246 |
+
result_tabs = gr.Tabs()
|
| 247 |
+
with result_tabs:
|
| 248 |
+
with gr.Tab("π Love Guide"):
|
| 249 |
+
love_guide_output = gr.Markdown()
|
| 250 |
+
|
| 251 |
+
with gr.Tab("πΎ Raw Analysis"):
|
| 252 |
+
raw_analysis = gr.JSON(label="Raw Analysis Data")
|
| 253 |
+
|
| 254 |
+
with gr.Tab("π₯ Download"):
|
| 255 |
+
download_output = gr.File(label="Download Love Guide")
|
| 256 |
+
|
| 257 |
+
status_output = gr.Textbox(label="Status", interactive=False)
|
| 258 |
+
|
| 259 |
+
# Event handlers
|
| 260 |
+
analyze_btn.click(
|
| 261 |
+
fn=generate_love_guide,
|
| 262 |
+
inputs=[person_name, file_upload],
|
| 263 |
+
outputs=[raw_analysis],
|
| 264 |
+
api_visibility="public"
|
| 265 |
+
).then(
|
| 266 |
+
fn=format_analysis,
|
| 267 |
+
inputs=[raw_analysis],
|
| 268 |
+
outputs=[love_guide_output]
|
| 269 |
+
).then(
|
| 270 |
+
fn=save_analysis,
|
| 271 |
+
inputs=[raw_analysis],
|
| 272 |
+
outputs=[download_output]
|
| 273 |
+
).then(
|
| 274 |
+
fn=lambda: "β
Love guide generated successfully! You can now view the results and download the guide.",
|
| 275 |
+
outputs=[status_output]
|
| 276 |
+
)
|
| 277 |
+
|
| 278 |
+
gr.Markdown("""
|
| 279 |
+
## π‘ Tips for Best Results
|
| 280 |
+
|
| 281 |
+
- Upload multiple documents for more accurate analysis
|
| 282 |
+
- Include both positive and challenging moments
|
| 283 |
+
- Be specific about the person's name
|
| 284 |
+
- The more context you provide, the better the guide will be
|
| 285 |
+
|
| 286 |
+
## β οΈ Privacy Note
|
| 287 |
+
|
| 288 |
+
All files are processed locally and not stored on our servers. Your relationship data remains private.
|
| 289 |
+
""")
|
| 290 |
+
|
| 291 |
+
# Launch the app with custom theme
|
| 292 |
+
demo.launch(
|
| 293 |
+
theme=custom_theme,
|
| 294 |
+
footer_links=[
|
| 295 |
+
{"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"},
|
| 296 |
+
{"label": "Gradio Documentation", "url": "https://www.gradio.app/docs"}
|
| 297 |
+
],
|
| 298 |
+
title="Love Guide Generator",
|
| 299 |
+
description="AI-powered relationship analysis and love guide generator"
|
| 300 |
+
)
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
PyMuPDF
|
| 2 |
+
Pillow
|
| 3 |
+
gradio>=6.0
|
| 4 |
+
pytesseract
|
| 5 |
+
requests
|
| 6 |
+
numpy
|
| 7 |
+
opencv-python
|