Sakshi2005's picture
Create app.py
89cd55d verified
raw
history blame
2.71 kB
# Install
import gradio as gr
import google.generativeai as genai
import json
# ✅ Configure Gemini API key
API_KEY = "AIzaSyAtm1yxPoXsz30KJUnyQNN9QeGw3FMIoMU"
genai.configure(api_key=API_KEY)
model = genai.GenerativeModel(model_name="gemini-2.0-flash")
# ✅ Analyze relationship chat from text input
def analyze_text(text):
prompt = f"""
You are a relationship AI assistant.
Analyze the chat text below and do all of the following:
1. Provide a brief sentiment/emotional summary.
2. Identify key events or milestones in the relationship.
3. Write a short, heartfelt relationship narrative.
4. Calculate a compatibility score from 0 to 100.
5. Provide a clear and friendly interpretation of that score.
Return ONLY a JSON object with the following keys:
"sentiment_summary", "key_events", "narrative", "compatibility_score", "interpretation"
Chat text:
\"\"\"{text}\"\"\"
"""
response = model.generate_content(prompt)
return response.text.strip()
# ✅ Process and return outputs
def process_chat(chat_text):
if not chat_text.strip():
return "", "", "", "", "", ""
analysis = analyze_text(chat_text)
try:
parsed = json.loads(analysis)
except Exception:
parsed = None
if parsed:
sentiment = parsed.get("sentiment_summary", "")
events = parsed.get("key_events", "")
narrative = parsed.get("narrative", "")
score = str(parsed.get("compatibility_score", ""))
interpretation = parsed.get("interpretation", "")
else:
sentiment = events = narrative = score = interpretation = ""
return analysis, sentiment, events, narrative, score, interpretation
# ✅ Gradio UI
with gr.Blocks() as demo:
gr.Markdown("# 💬 Relationship Chat Text Analyzer with Gemini 2.0 Flash")
chat_input = gr.Textbox(label="Paste your chat here", lines=20, placeholder="Paste chat content here...")
raw_output = gr.Textbox(label="Raw AI Analysis Response", lines=10, interactive=False)
sentiment_output = gr.Textbox(label="Sentiment Summary", interactive=False)
events_output = gr.Textbox(label="Key Events / Milestones", interactive=False)
narrative_output = gr.Textbox(label="Relationship Narrative", interactive=False)
score_output = gr.Textbox(label="Compatibility Score", interactive=False)
interpretation_output = gr.Textbox(label="Interpretation", interactive=False)
analyze_btn = gr.Button("Analyze Relationship")
analyze_btn.click(process_chat, inputs=chat_input, outputs=[
raw_output,
sentiment_output,
events_output,
narrative_output,
score_output,
interpretation_output,
])
# ✅ Launch app
demo.launch()