Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,98 +1,102 @@
|
|
| 1 |
import os
|
| 2 |
import json
|
|
|
|
| 3 |
import requests
|
| 4 |
-
import
|
|
|
|
| 5 |
from groq import Groq
|
| 6 |
-
import streamlit.components.v1 as components
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
# ==========================================
|
| 11 |
-
st.set_page_config(
|
| 12 |
-
page_title="GyanChai.in",
|
| 13 |
-
page_icon="💬",
|
| 14 |
-
layout="wide",
|
| 15 |
-
initial_sidebar_state="collapsed"
|
| 16 |
-
)
|
| 17 |
|
| 18 |
# ==========================================
|
| 19 |
-
#
|
| 20 |
# ==========================================
|
| 21 |
try:
|
| 22 |
groq_client = Groq(api_key=os.environ["GROQ_API_KEY"])
|
| 23 |
SERPER_API_KEY = os.environ["SERPER_API_KEY"]
|
| 24 |
-
except KeyError
|
| 25 |
-
|
| 26 |
-
st.stop()
|
| 27 |
|
| 28 |
# ==========================================
|
| 29 |
-
#
|
| 30 |
# ==========================================
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
# ==========================================
|
| 37 |
-
#
|
| 38 |
# ==========================================
|
| 39 |
SYSTEM_PROMPT = """You are GyanChai.in, a large language model.
|
| 40 |
|
| 41 |
-
CRITICAL IDENTITY RULES
|
| 42 |
1. If user asks "who are you", "what are you", "your name", respond EXACTLY: I am GyanChai.in, a large language model. I am here to help you. How can I help you?
|
| 43 |
2. ONLY if user explicitly asks "who created you", "who is your developer", "who made you", "who built you", then respond: My developer is Rajdeep Pradhan.
|
| 44 |
-
3. In all other cases, NEVER mention any developer name
|
| 45 |
-
4. Detect user language and respond in
|
| 46 |
-
5.
|
| 47 |
-
6.
|
| 48 |
-
7.
|
| 49 |
-
8.
|
| 50 |
|
| 51 |
# ==========================================
|
| 52 |
-
#
|
| 53 |
# ==========================================
|
| 54 |
def web_search(query: str) -> str:
|
| 55 |
try:
|
| 56 |
url = "https://google.serper.dev/search"
|
| 57 |
-
headers = {
|
| 58 |
-
"X-API-KEY": SERPER_API_KEY,
|
| 59 |
-
"Content-Type": "application/json"
|
| 60 |
-
}
|
| 61 |
payload = json.dumps({"q": query, "num": 5})
|
| 62 |
response = requests.post(url, headers=headers, data=payload, timeout=10)
|
| 63 |
-
response.raise_for_status()
|
| 64 |
data = response.json()
|
| 65 |
-
|
| 66 |
results = []
|
| 67 |
if "organic" in data:
|
| 68 |
for item in data["organic"][:3]:
|
| 69 |
-
title
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
return "\n".join(results) if results else "No relevant search results found."
|
| 74 |
-
except Exception as e:
|
| 75 |
-
return f"Search service unavailable: {str(e)}"
|
| 76 |
|
| 77 |
# ==========================================
|
| 78 |
-
#
|
| 79 |
# ==========================================
|
| 80 |
-
|
|
|
|
| 81 |
try:
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
|
| 92 |
-
# Build
|
| 93 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 94 |
-
messages.extend(
|
| 95 |
-
messages.append({"role": "user", "content":
|
| 96 |
|
| 97 |
# Call Groq
|
| 98 |
completion = groq_client.chat.completions.create(
|
|
@@ -101,116 +105,53 @@ def generate_ai_response(user_input: str, chat_history: list) -> str:
|
|
| 101 |
temperature=0.7,
|
| 102 |
max_tokens=1000
|
| 103 |
)
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
except Exception as e:
|
| 107 |
-
return
|
| 108 |
|
| 109 |
# ==========================================
|
| 110 |
-
#
|
| 111 |
# ==========================================
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
* { margin: 0; padding: 0; box-sizing: border-box; }
|
| 120 |
-
body {
|
| 121 |
-
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
|
| 122 |
-
background: #0b141a;
|
| 123 |
-
overflow: hidden;
|
| 124 |
-
}
|
| 125 |
-
|
| 126 |
-
.app-container {
|
| 127 |
-
width: 100%;
|
| 128 |
-
height: 100vh;
|
| 129 |
-
display: flex;
|
| 130 |
-
flex-direction: column;
|
| 131 |
-
background: #0b141a;
|
| 132 |
-
}
|
| 133 |
-
|
| 134 |
-
/* Header Bar */
|
| 135 |
-
.header {
|
| 136 |
-
background: #202c33;
|
| 137 |
-
padding: 10px 16px;
|
| 138 |
-
display: flex;
|
| 139 |
-
align-items: center;
|
| 140 |
-
gap: 15px;
|
| 141 |
-
border-bottom: 1px solid #2a3942;
|
| 142 |
-
min-height: 60px;
|
| 143 |
-
}
|
| 144 |
-
.avatar {
|
| 145 |
-
width: 40px;
|
| 146 |
-
height: 40px;
|
| 147 |
-
border-radius: 50%;
|
| 148 |
-
background: linear-gradient(135deg, #00a884, #008069);
|
| 149 |
-
display: flex;
|
| 150 |
-
align-items: center;
|
| 151 |
-
justify-content: center;
|
| 152 |
-
color: white;
|
| 153 |
-
font-weight: 600;
|
| 154 |
-
font-size: 18px;
|
| 155 |
-
}
|
| 156 |
-
.header-details {
|
| 157 |
-
flex: 1;
|
| 158 |
-
}
|
| 159 |
-
.contact-name {
|
| 160 |
-
color: #e9edef;
|
| 161 |
-
font-size: 16px;
|
| 162 |
-
font-weight: 500;
|
| 163 |
-
line-height: 21px;
|
| 164 |
-
}
|
| 165 |
-
.contact-status {
|
| 166 |
-
color: #8696a0;
|
| 167 |
-
font-size: 13px;
|
| 168 |
-
line-height: 20px;
|
| 169 |
-
display: flex;
|
| 170 |
-
align-items: center;
|
| 171 |
-
gap: 5px;
|
| 172 |
-
}
|
| 173 |
-
.header-actions {
|
| 174 |
-
display: flex;
|
| 175 |
-
gap: 20px;
|
| 176 |
-
align-items: center;
|
| 177 |
-
}
|
| 178 |
-
.icon-button {
|
| 179 |
-
width: 40px;
|
| 180 |
-
height: 40px;
|
| 181 |
-
border-radius: 50%;
|
| 182 |
-
border: none;
|
| 183 |
-
background: transparent;
|
| 184 |
-
color: #aebac1;
|
| 185 |
-
cursor: pointer;
|
| 186 |
-
display: flex;
|
| 187 |
-
align-items: center;
|
| 188 |
-
justify-content: center;
|
| 189 |
-
transition: all 0.2s;
|
| 190 |
-
}
|
| 191 |
-
.icon-button:hover {
|
| 192 |
-
background: rgba(134, 150, 160, 0.15);
|
| 193 |
-
}
|
| 194 |
-
.icon-button.active {
|
| 195 |
-
color: #00a884;
|
| 196 |
-
}
|
| 197 |
-
.icon-button.live-active {
|
| 198 |
-
color: #ff0000;
|
| 199 |
-
}
|
| 200 |
-
|
| 201 |
-
/* Live Badge */
|
| 202 |
-
.live-indicator {
|
| 203 |
-
background: #ff0000;
|
| 204 |
-
color: white;
|
| 205 |
-
padding: 2px 8px;
|
| 206 |
-
border-radius: 10px;
|
| 207 |
-
font-size: 11px;
|
| 208 |
-
font-weight: 700;
|
| 209 |
-
animation: blink 1.5s infinite;
|
| 210 |
-
}
|
| 211 |
-
@keyframes blink {
|
| 212 |
-
0%, 100% { opacity: 1; }
|
| 213 |
-
50% { opacity: 0.6; }
|
| 214 |
-
}
|
| 215 |
-
|
| 216 |
-
/* Chat
|
|
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
+
import re
|
| 4 |
import requests
|
| 5 |
+
from flask import Flask, request, jsonify, Response
|
| 6 |
+
from flask_cors import CORS
|
| 7 |
from groq import Groq
|
|
|
|
| 8 |
|
| 9 |
+
app = Flask(__name__)
|
| 10 |
+
CORS(app)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# ==========================================
|
| 13 |
+
# API CLIENTS
|
| 14 |
# ==========================================
|
| 15 |
try:
|
| 16 |
groq_client = Groq(api_key=os.environ["GROQ_API_KEY"])
|
| 17 |
SERPER_API_KEY = os.environ["SERPER_API_KEY"]
|
| 18 |
+
except KeyError:
|
| 19 |
+
raise Exception("GROQ_API_KEY or SERPER_API_KEY missing")
|
|
|
|
| 20 |
|
| 21 |
# ==========================================
|
| 22 |
+
# EMOJI FILTER FOR TTS
|
| 23 |
# ==========================================
|
| 24 |
+
def filter_emoji_for_tts(text: str) -> str:
|
| 25 |
+
"""Remove all emojis and special unicode for TTS reading"""
|
| 26 |
+
emoji_pattern = re.compile(
|
| 27 |
+
"["
|
| 28 |
+
"\U0001F600-\U0001F64F" # emoticons
|
| 29 |
+
"\U0001F300-\U0001F5FF" # symbols & pictographs
|
| 30 |
+
"\U0001F680-\U0001F6FF" # transport & map symbols
|
| 31 |
+
"\U0001F1E0-\U0001F1FF" # flags
|
| 32 |
+
"\U00002702-\U000027B0"
|
| 33 |
+
"\U000024C2-\U0001F251"
|
| 34 |
+
"\U0001f926-\U0001f937"
|
| 35 |
+
"\U00010000-\U0010ffff"
|
| 36 |
+
"\u2640-\u2642"
|
| 37 |
+
"\u2600-\u2B55"
|
| 38 |
+
"\u200d"
|
| 39 |
+
"\u23cf"
|
| 40 |
+
"\u23e9"
|
| 41 |
+
"\u231a"
|
| 42 |
+
"\ufe0f" # dingbats
|
| 43 |
+
"\u3030"
|
| 44 |
+
"]+", flags=re.UNICODE)
|
| 45 |
+
return emoji_pattern.sub(r'', text).strip()
|
| 46 |
|
| 47 |
# ==========================================
|
| 48 |
+
# SYSTEM PROMPT - RAJDEEP RULE
|
| 49 |
# ==========================================
|
| 50 |
SYSTEM_PROMPT = """You are GyanChai.in, a large language model.
|
| 51 |
|
| 52 |
+
CRITICAL IDENTITY RULES:
|
| 53 |
1. If user asks "who are you", "what are you", "your name", respond EXACTLY: I am GyanChai.in, a large language model. I am here to help you. How can I help you?
|
| 54 |
2. ONLY if user explicitly asks "who created you", "who is your developer", "who made you", "who built you", then respond: My developer is Rajdeep Pradhan.
|
| 55 |
+
3. In all other cases, NEVER mention any developer name or Rajdeep Pradhan.
|
| 56 |
+
4. Detect user language and respond in same language.
|
| 57 |
+
5. You can use emojis in text responses to be friendly, but keep them minimal.
|
| 58 |
+
6. If you lack verified data, state: I do not have that information.
|
| 59 |
+
7. Never claim: video calls, voice calls, file access, image generation. You are text-based only.
|
| 60 |
+
8. Be concise and truthful."""
|
| 61 |
|
| 62 |
# ==========================================
|
| 63 |
+
# WEB SEARCH
|
| 64 |
# ==========================================
|
| 65 |
def web_search(query: str) -> str:
|
| 66 |
try:
|
| 67 |
url = "https://google.serper.dev/search"
|
| 68 |
+
headers = {"X-API-KEY": SERPER_API_KEY, "Content-Type": "application/json"}
|
|
|
|
|
|
|
|
|
|
| 69 |
payload = json.dumps({"q": query, "num": 5})
|
| 70 |
response = requests.post(url, headers=headers, data=payload, timeout=10)
|
|
|
|
| 71 |
data = response.json()
|
|
|
|
| 72 |
results = []
|
| 73 |
if "organic" in data:
|
| 74 |
for item in data["organic"][:3]:
|
| 75 |
+
results.append(f"- {item.get('title', '')}: {item.get('snippet', '')}")
|
| 76 |
+
return "\n".join(results) if results else "No results found."
|
| 77 |
+
except:
|
| 78 |
+
return "Search unavailable."
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
# ==========================================
|
| 81 |
+
# CHAT ENDPOINT
|
| 82 |
# ==========================================
|
| 83 |
+
@app.route("/api/chat", methods=["POST"])
|
| 84 |
+
def chat():
|
| 85 |
try:
|
| 86 |
+
data = request.json
|
| 87 |
+
user_message = data.get("message", "")
|
| 88 |
+
history = data.get("history", [])
|
| 89 |
|
| 90 |
+
# Web search if needed
|
| 91 |
+
search_keywords = ["search", "latest", "news", "current", "weather", "today", "price", "who is", "what is"]
|
| 92 |
+
if any(k in user_message.lower() for k in search_keywords):
|
| 93 |
+
search_res = web_search(user_message)
|
| 94 |
+
user_message = f"{user_message}\n\n[Web Results]\n{search_res}"
|
| 95 |
|
| 96 |
+
# Build messages
|
| 97 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 98 |
+
messages.extend(history[-10:])
|
| 99 |
+
messages.append({"role": "user", "content": user_message})
|
| 100 |
|
| 101 |
# Call Groq
|
| 102 |
completion = groq_client.chat.completions.create(
|
|
|
|
| 105 |
temperature=0.7,
|
| 106 |
max_tokens=1000
|
| 107 |
)
|
| 108 |
+
|
| 109 |
+
ai_response = completion.choices[0].message.content
|
| 110 |
+
|
| 111 |
+
return jsonify({
|
| 112 |
+
"response": ai_response,
|
| 113 |
+
"response_tts": filter_emoji_for_tts(ai_response), # Emoji filtered for TTS
|
| 114 |
+
"status": "success"
|
| 115 |
+
})
|
| 116 |
+
except Exception as e:
|
| 117 |
+
return jsonify({"response": f"Error: {str(e)}", "status": "error"}), 500
|
| 118 |
+
|
| 119 |
+
# ==========================================
|
| 120 |
+
# TTS ENDPOINT - GROQ PLAYAI TTS
|
| 121 |
+
# ==========================================
|
| 122 |
+
@app.route("/api/tts", methods=["POST"])
|
| 123 |
+
def text_to_speech():
|
| 124 |
+
try:
|
| 125 |
+
data = request.json
|
| 126 |
+
text = data.get("text", "")
|
| 127 |
+
|
| 128 |
+
# Filter emojis before TTS
|
| 129 |
+
clean_text = filter_emoji_for_tts(text)
|
| 130 |
+
|
| 131 |
+
if not clean_text:
|
| 132 |
+
return jsonify({"error": "No text to speak"}), 400
|
| 133 |
+
|
| 134 |
+
# Groq TTS API
|
| 135 |
+
response = groq_client.audio.speech.create(
|
| 136 |
+
model="playai-tts",
|
| 137 |
+
voice="Aaliyah-PlayAI",
|
| 138 |
+
input=clean_text,
|
| 139 |
+
response_format="mp3"
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
# Return audio stream
|
| 143 |
+
return Response(response.read(), mimetype="audio/mpeg")
|
| 144 |
|
| 145 |
except Exception as e:
|
| 146 |
+
return jsonify({"error": str(e)}), 500
|
| 147 |
|
| 148 |
# ==========================================
|
| 149 |
+
# HEALTH CHECK
|
| 150 |
# ==========================================
|
| 151 |
+
@app.route("/api/health", methods=["GET"])
|
| 152 |
+
def health():
|
| 153 |
+
return jsonify({"status": "GyanChai.in Brain running"})
|
| 154 |
+
|
| 155 |
+
if __name__ == "__main__":
|
| 156 |
+
port = int(os.environ.get("PORT", 7860))
|
| 157 |
+
app.run(host="0.0.0.0", port=port, debug=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|