AumCoreAI commited on
Commit
93226a5
·
1 Parent(s): df954f5

DEPLOY: Final Premium FastAPI UI Interface

Browse files
Files changed (1) hide show
  1. app.py +155 -35
app.py CHANGED
@@ -1,38 +1,158 @@
1
- import gradio as gr
2
  import os
3
- from main import AICore
4
-
5
- # API Key check
6
- api_key = os.environ.get("GROQ_API_KEY", "Yahan_Apni_Groq_Key_Dalo_Ya_HF_Secret_Use_Karo")
7
- ai = AICore(api_key=api_key)
8
-
9
- def chat_func(text, img, history):
10
- if not text and not img:
11
- return "", history
12
-
13
- # AI se response lena
14
- response = ai.get_response(text or "Describe this image", img)
15
-
16
- # History update
17
- history.append((text or "Image Sent", response))
18
- return "", history
19
-
20
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
21
- gr.Markdown("## 🤖 AumCore-M7B Real AI Vision")
22
-
23
- chatbot = gr.Chatbot(height=500)
24
- with gr.Row():
25
- with gr.Column(scale=8):
26
- txt = gr.Textbox(
27
- show_label=False,
28
- placeholder="AumCore se kuch bhi puchiye...",
29
- container=False
30
- )
31
- with gr.Column(scale=2):
32
- img = gr.Image(type="filepath", label="Vision")
33
-
34
- txt.submit(chat_func, [txt, img, chatbot], [txt, chatbot])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
  if __name__ == "__main__":
37
- # HF Space port 7860 use karta hai
38
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
 
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
+ SYSTEM_PROMPT = f"""
34
+ You are AumCore AI, a high-level artificial intelligence system.
35
+ User Identity: {USERNAME}.
36
+ Core Rules:
37
+ 1. Reply in 60% English and 40% Hindi (Devanagari).
38
+ 2. Keep responses powerful, concise, and direct (Max 3 lines).
39
+ 3. Do NOT use any labels like 'Aum:', 'Sanjay:', or 'Assistant:' in the output.
40
+ 4. If code is requested, provide it in clear Markdown blocks.
41
+ """
42
+
43
+ HTML_UI = '''
44
+ <!DOCTYPE html>
45
+ <html lang="en">
46
+ <head>
47
+ <meta charset="UTF-8">
48
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
49
+ <title>AumCore AI - Final Build</title>
50
+ <script src="https://cdn.tailwindcss.com"></script>
51
+ <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
52
+ <style>
53
+ @import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;600&family=Fira+Code:wght@400;500&display=swap');
54
+ body { background-color: #0d1117; color: #c9d1d9; font-family: 'Inter', sans-serif; display: flex; height: 100vh; overflow: hidden; margin: 0; }
55
+ .sidebar { width: 260px; background: #010409; border-right: 1px solid #30363d; display: flex; flex-direction: column; padding: 15px; flex-shrink: 0; }
56
+ .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; }
57
+ .nav-item:hover { background: #161b22; color: white; }
58
+ .new-chat-btn { background: #238636; color: white !important; font-weight: 600; margin-bottom: 20px; }
59
+ .main-chat { flex: 1; display: flex; flex-direction: column; background: #0d1117; position: relative; }
60
+ .chat-box { flex: 1; overflow-y: auto; display: flex; flex-direction: column; align-items: center; padding: 60px 20px 120px 20px; scroll-behavior: smooth; }
61
+ .message-wrapper { width: 100%; max-width: 760px; display: flex; flex-direction: column; margin-bottom: 35px; animation: fadeIn 0.3s ease; }
62
+ @keyframes fadeIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }
63
+ .bubble { padding: 5px 0; font-size: 17px; line-height: 1.8; width: 100%; word-wrap: break-word; white-space: pre-wrap; }
64
+ .user-text { color: #58a6ff; font-weight: 600; }
65
+ .ai-text { color: #e6edf3; }
66
+ pre { background: #161b22; padding: 18px; border-radius: 12px; border: 1px solid #30363d; margin: 15px 0; overflow-x: auto; width: 100%; }
67
+ code { font-family: 'Fira Code', monospace; color: #79c0ff; font-size: 14px; }
68
+ .input-area { position: absolute; bottom: 0; left: 0; width: 100%; background: linear-gradient(transparent, #0d1117 30%); padding-bottom: 30px; }
69
+ .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; }
70
+ #user-input { background: transparent; flex: 1; outline: none; border: none; color: white; font-size: 16px; resize: none; max-height: 200px; min-height: 28px; }
71
+ .send-btn { color: #58a6ff; transition: transform 0.1s ease; margin-bottom: 4px; }
72
+ </style>
73
+ </head>
74
+ <body>
75
+ <div class="sidebar">
76
+ <button class="nav-item new-chat-btn" onclick="window.location.reload()"><i class="fas fa-plus"></i> New Chat</button>
77
+ <div class="nav-item"><i class="fas fa-history"></i> History</div>
78
+ <div class="mt-auto">
79
+ <div class="nav-item reset-btn" onclick="confirmReset()"><i class="fas fa-trash-alt"></i> Reset Memory</div>
80
+ <div class="nav-item"><i class="fas fa-cog"></i> Settings</div>
81
+ </div>
82
+ </div>
83
+ <div class="main-chat">
84
+ <div id="chat-log" class="chat-box"></div>
85
+ <div class="input-area">
86
+ <div class="input-container">
87
+ <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>
88
+ <button onclick="send()" class="send-btn"><i class="fas fa-paper-plane fa-lg"></i></button>
89
+ </div>
90
+ </div>
91
+ </div>
92
+ <script>
93
+ async function confirmReset() {
94
+ if(confirm("Sanjay bhai, memory clear karein?")) {
95
+ await fetch('/reset', { method: 'POST' });
96
+ window.location.reload();
97
+ }
98
+ }
99
+ async function send() {
100
+ const input = document.getElementById('user-input');
101
+ const log = document.getElementById('chat-log');
102
+ const text = input.value.trim();
103
+ if(!text) return;
104
+ log.innerHTML += `<div class="message-wrapper"><div class="bubble user-text">${text}</div></div>`;
105
+ input.value = ''; input.style.height = 'auto';
106
+ log.scrollTop = log.scrollHeight;
107
+ try {
108
+ const res = await fetch('/chat', {
109
+ method: 'POST',
110
+ headers: {'Content-Type': 'application/x-www-form-urlencoded'},
111
+ body: 'message=' + encodeURIComponent(text)
112
+ });
113
+ const data = await res.json();
114
+ let formatted = data.response.replace(/```([\s\S]*?)```/g, '<pre><code>$1</code></pre>');
115
+ log.innerHTML += `<div class="message-wrapper"><div class="bubble ai-text">${formatted}</div></div>`;
116
+ } catch (e) {
117
+ log.innerHTML += `<div class="message-wrapper text-red-500">Error connecting to AumCore.</div>`;
118
+ }
119
+ log.scrollTop = log.scrollHeight;
120
+ }
121
+ </script>
122
+ </body>
123
+ </html>
124
+ '''
125
+
126
+ @app.get("/", response_class=HTMLResponse)
127
+ async def get_ui(): return HTML_UI
128
+
129
+ @app.post("/reset")
130
+ async def reset():
131
+ save_memory([])
132
+ return {"message": "Memory clear ho gayi!"}
133
+
134
+ @app.post("/chat")
135
+ async def chat(message: str = Form(...)):
136
+ memory_data = load_memory()
137
+ history = memory_data.get("history", [])[-8:]
138
+ api_messages = [{"role": "system", "content": SYSTEM_PROMPT}]
139
+ for chat_pair in history:
140
+ api_messages.append({"role": "user", "content": chat_pair["u"]})
141
+ api_messages.append({"role": "assistant", "content": chat_pair["a"]})
142
+ api_messages.append({"role": "user", "content": message})
143
+ try:
144
+ completion = client.chat.completions.create(
145
+ model="llama-3.3-70b-versatile",
146
+ messages=api_messages,
147
+ temperature=0.6,
148
+ max_tokens=500
149
+ )
150
+ ai_response = completion.choices[0].message.content.strip()
151
+ history.append({"u": message, "a": ai_response})
152
+ save_memory(history)
153
+ return {"response": ai_response}
154
+ except Exception as e:
155
+ return {"response": f"Error: {str(e)}"}
156
 
157
  if __name__ == "__main__":
158
+ uvicorn.run(app, host="0.0.0.0", port=7860)