Sakshi2005 commited on
Commit
89cd55d
·
verified ·
1 Parent(s): 3c65c74

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Install
2
+ import gradio as gr
3
+ import google.generativeai as genai
4
+ import json
5
+
6
+ # ✅ Configure Gemini API key
7
+ API_KEY = "AIzaSyAtm1yxPoXsz30KJUnyQNN9QeGw3FMIoMU"
8
+ genai.configure(api_key=API_KEY)
9
+ model = genai.GenerativeModel(model_name="gemini-2.0-flash")
10
+
11
+ # ✅ Analyze relationship chat from text input
12
+ def analyze_text(text):
13
+ prompt = f"""
14
+ You are a relationship AI assistant.
15
+
16
+ Analyze the chat text below and do all of the following:
17
+
18
+ 1. Provide a brief sentiment/emotional summary.
19
+ 2. Identify key events or milestones in the relationship.
20
+ 3. Write a short, heartfelt relationship narrative.
21
+ 4. Calculate a compatibility score from 0 to 100.
22
+ 5. Provide a clear and friendly interpretation of that score.
23
+
24
+ Return ONLY a JSON object with the following keys:
25
+ "sentiment_summary", "key_events", "narrative", "compatibility_score", "interpretation"
26
+
27
+ Chat text:
28
+ \"\"\"{text}\"\"\"
29
+ """
30
+ response = model.generate_content(prompt)
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
+ parsed = json.loads(analysis)
42
+ except Exception:
43
+ parsed = None
44
+
45
+ if parsed:
46
+ sentiment = parsed.get("sentiment_summary", "")
47
+ events = parsed.get("key_events", "")
48
+ narrative = parsed.get("narrative", "")
49
+ score = str(parsed.get("compatibility_score", ""))
50
+ interpretation = parsed.get("interpretation", "")
51
+ else:
52
+ sentiment = events = narrative = score = interpretation = ""
53
+
54
+ return analysis, sentiment, events, narrative, score, interpretation
55
+
56
+ # ✅ Gradio UI
57
+ with gr.Blocks() as demo:
58
+ gr.Markdown("# 💬 Relationship Chat Text Analyzer with Gemini 2.0 Flash")
59
+
60
+ chat_input = gr.Textbox(label="Paste your chat here", lines=20, placeholder="Paste chat content here...")
61
+
62
+ raw_output = gr.Textbox(label="Raw AI Analysis Response", lines=10, interactive=False)
63
+ sentiment_output = gr.Textbox(label="Sentiment Summary", interactive=False)
64
+ events_output = gr.Textbox(label="Key Events / Milestones", interactive=False)
65
+ narrative_output = gr.Textbox(label="Relationship Narrative", interactive=False)
66
+ score_output = gr.Textbox(label="Compatibility Score", interactive=False)
67
+ interpretation_output = gr.Textbox(label="Interpretation", interactive=False)
68
+
69
+ analyze_btn = gr.Button("Analyze Relationship")
70
+
71
+ analyze_btn.click(process_chat, inputs=chat_input, outputs=[
72
+ raw_output,
73
+ sentiment_output,
74
+ events_output,
75
+ narrative_output,
76
+ score_output,
77
+ interpretation_output,
78
+ ])
79
+
80
+ # ✅ Launch app
81
+ demo.launch()