Files changed (1) hide show
  1. app.py +148 -3
app.py CHANGED
@@ -1,3 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  HTML_UI = '''
2
  <!DOCTYPE html>
3
  <html lang="en">
@@ -8,11 +51,113 @@ HTML_UI = '''
8
  <script src="https://cdn.tailwindcss.com"></script>
9
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
10
  <style>
11
- /* Tumhara original CSS yaha aayega */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  </style>
13
  </head>
14
  <body>
15
- <!-- Tumhara original UI structure yaha aayega -->
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  </body>
17
  </html>
18
- '''
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import uvicorn
3
+ import json
4
+ from fastapi import FastAPI, Form
5
+ from fastapi.responses import HTMLResponse, JSONResponse
6
+ from groq import Groq
7
+
8
+ # --- CORE INITIALIZATION ---
9
+ app = FastAPI()
10
+ client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
11
+
12
+ # Configuration & Persistent Memory Setup
13
+ USERNAME = "Sanjay"
14
+ MEMORY_FILE = "memory.json"
15
+
16
+ def load_memory():
17
+ if os.path.exists(MEMORY_FILE):
18
+ try:
19
+ with open(MEMORY_FILE, 'r') as f:
20
+ data = json.load(f)
21
+ return data if "history" in data else {"history": []}
22
+ except Exception as e:
23
+ return {"history": []}
24
+ return {"history": []}
25
+
26
+ def save_memory(chat_history):
27
+ try:
28
+ with open(MEMORY_FILE, 'w') as f:
29
+ json.dump({"history": chat_history}, f, indent=4)
30
+ except Exception as e:
31
+ print(f"Memory Save Error: {e}")
32
+
33
+ # --- STRICT PERSONA SETTINGS (MEMORY PRESERVED) ---
34
+ SYSTEM_PROMPT = f"""
35
+ Role: Senior AI Architect (AumCore AI).
36
+ User Identity: {USERNAME}.
37
+ Core Rules:
38
+ 1. Language: Always 60% English and 40% Hindi (Devanagari).
39
+ 2. Code Rule: If {USERNAME} asks for code, provide a robust production-ready Python script with try-except blocks.
40
+ 3. Length: Responses must be powerful and direct (Max 4 lines).
41
+ 4. Persona: Talk like a Master AI, not a basic chatbot.
42
+ """
43
+
44
  HTML_UI = '''
45
  <!DOCTYPE html>
46
  <html lang="en">
 
51
  <script src="https://cdn.tailwindcss.com"></script>
52
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
53
  <style>
54
+ @import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;600&family=Fira+Code:wght@400;500&display=swap');
55
+ body { background-color: #0d1117; color: #c9d1d9; font-family: 'Inter', sans-serif; display: flex; height: 100vh; overflow: hidden; margin: 0; }
56
+ .sidebar { width: 260px; background: #010409; border-right: 1px solid #30363d; display: flex; flex-direction: column; padding: 15px; flex-shrink: 0; }
57
+ .nav-item { padding: 12px; margin-bottom: 5px; border-radius: 8px; cursor: pointer; display: flex; align-items: center; gap: 12px; color: #8b949e; transition: all 0.2s ease; }
58
+ .nav-item:hover { background: #161b22; color: white; }
59
+ .new-chat-btn { background: #238636; color: white !important; font-weight: 600; margin-bottom: 20px; }
60
+ .main-chat { flex: 1; display: flex; flex-direction: column; background: #0d1117; position: relative; }
61
+ .chat-box { flex: 1; overflow-y: auto; display: flex; flex-direction: column; align-items: center; padding: 60px 20px 120px 20px; scroll-behavior: smooth; }
62
+ .message-wrapper { width: 100%; max-width: 760px; display: flex; flex-direction: column; margin-bottom: 35px; animation: fadeIn 0.3s ease; }
63
+ @keyframes fadeIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }
64
+ .bubble { padding: 5px 0; font-size: 17px; line-height: 1.8; width: 100%; word-wrap: break-word; white-space: pre-wrap; }
65
+ .user-text { color: #58a6ff; font-weight: 600; }
66
+ .ai-text { color: #e6edf3; }
67
+ pre { background: #161b22; padding: 18px; border-radius: 12px; border: 1px solid #30363d; margin: 15px 0; overflow-x: auto; width: 100%; }
68
+ code { font-family: 'Fira Code', monospace; color: #79c0ff; font-size: 14px; }
69
+ .input-area { position: absolute; bottom: 0; left: 0; width: 100%; background: linear-gradient(transparent, #0d1117 30%); padding-bottom: 30px; }
70
+ .input-container { max-width: 800px; margin: 0 auto; background: #161b22; border: 1px solid #30363d; border-radius: 16px; padding: 12px 18px; display: flex; align-items: flex-end; gap: 12px; }
71
+ #user-input { background: transparent; flex: 1; outline: none; border: none; color: white; font-size: 16px; resize: none; max-height: 200px; min-height: 28px; }
72
+ .send-btn { color: #58a6ff; transition: transform 0.1s ease; margin-bottom: 4px; }
73
  </style>
74
  </head>
75
  <body>
76
+ <div class="sidebar">
77
+ <button class="nav-item new-chat-btn" onclick="window.location.reload()"><i class="fas fa-plus"></i> New Chat</button>
78
+ <div class="nav-item"><i class="fas fa-history"></i> History</div>
79
+ <div class="mt-auto">
80
+ <div class="nav-item reset-btn" onclick="confirmReset()"><i class="fas fa-trash-alt"></i> Reset Memory</div>
81
+ <div class="nav-item"><i class="fas fa-cog"></i> Settings</div>
82
+ </div>
83
+ </div>
84
+ <div class="main-chat">
85
+ <div id="chat-log" class="chat-box"></div>
86
+ <div class="input-area">
87
+ <div class="input-container">
88
+ <textarea id="user-input" rows="1" placeholder="Type your message to AumCore..." oninput="this.style.height='auto';this.style.height=this.scrollHeight+'px'" onkeydown="if(event.key==='Enter'&&!event.shiftKey){event.preventDefault();send();}"></textarea>
89
+ <button onclick="send()" class="send-btn"><i class="fas fa-paper-plane fa-lg"></i></button>
90
+ </div>
91
+ </div>
92
+ </div>
93
+ <script>
94
+ async function confirmReset() {
95
+ if(confirm("Sanjay bhai, memory clear karein?")) {
96
+ await fetch('/reset', { method: 'POST' });
97
+ window.location.reload();
98
+ }
99
+ }
100
+ async function send() {
101
+ const input = document.getElementById('user-input');
102
+ const log = document.getElementById('chat-log');
103
+ const text = input.value.trim();
104
+ if(!text) return;
105
+ log.innerHTML += `<div class="message-wrapper"><div class="bubble user-text">${text}</div></div>`;
106
+ input.value = ''; input.style.height = 'auto';
107
+ log.scrollTop = log.scrollHeight;
108
+ try {
109
+ const res = await fetch('/chat', {
110
+ method: 'POST',
111
+ headers: {'Content-Type': 'application/x-www-form-urlencoded'},
112
+ body: 'message=' + encodeURIComponent(text)
113
+ });
114
+ const data = await res.json();
115
+ let formatted = data.response.replace(/```([\s\S]*?)```/g, '<pre><code>$1</code></pre>');
116
+ log.innerHTML += `<div class="message-wrapper"><div class="bubble ai-text">${formatted}</div></div>`;
117
+ } catch (e) {
118
+ log.innerHTML += `<div class="message-wrapper text-red-500">Error connecting to AumCore.</div>`;
119
+ }
120
+ log.scrollTop = log.scrollHeight;
121
+ }
122
+ </script>
123
  </body>
124
  </html>
125
+ '''
126
+
127
+ @app.get("/", response_class=HTMLResponse)
128
+ async def get_ui():
129
+ return HTML_UI
130
+
131
+ @app.post("/reset")
132
+ async def reset():
133
+ save_memory([])
134
+ return {"message": "Memory clear ho gayi!"}
135
+
136
+ @app.post("/chat")
137
+ async def chat(message: str = Form(...)):
138
+ memory_data = load_memory()
139
+ history = memory_data.get("history", [])[-10:] # Increased history context
140
+
141
+ api_messages = [{"role": "system", "content": SYSTEM_PROMPT}]
142
+ for chat_pair in history:
143
+ api_messages.append({"role": "user", "content": chat_pair["u"]})
144
+ api_messages.append({"role": "assistant", "content": chat_pair["a"]})
145
+
146
+ api_messages.append({"role": "user", "content": message})
147
+
148
+ try:
149
+ completion = client.chat.completions.create(
150
+ model="llama-3.3-70b-versatile",
151
+ messages=api_messages,
152
+ temperature=0.3, # Balanced for creativity and rule-following
153
+ max_tokens=800
154
+ )
155
+ ai_response = completion.choices[0].message.content.strip()
156
+ history.append({"u": message, "a": ai_response})
157
+ save_memory(history)
158
+ return {"response": ai_response}
159
+ except Exception as e:
160
+ return {"response": f"Error: {str(e)}"}
161
+
162
+ if __name__ == "__main__":
163
+ uvicorn.run(app, host="0.0.0.0", port=7860)