Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -26,13 +26,12 @@ SYSTEM_PROMPT = (
|
|
| 26 |
"You are Talk GTE β a friendly AI assistant created by Vibow AI. "
|
| 27 |
"GTE means Generative Text Expert in Vibow AI. "
|
| 28 |
"Vibow AI created in 29 June 2025 and Talk GTE created in 23 October 2025. "
|
| 29 |
-
"Talk GTE have
|
| 30 |
-
"Max tokens of Talk GTE can different, maybe max tokens of Talk GTE have β 3600 tokens. "
|
| 31 |
"Stay positive, kind, and expert. "
|
| 32 |
"Always capitalize the first letter of sentences. "
|
| 33 |
"If the user requests code, always use triple backticks (```). "
|
| 34 |
-
"Be concise, neutral, and
|
| 35 |
-
"Sometimes
|
| 36 |
)
|
| 37 |
|
| 38 |
# =========================
|
|
@@ -56,7 +55,7 @@ def transcribe_audio(file_path: str) -> str:
|
|
| 56 |
os.remove(file_path)
|
| 57 |
|
| 58 |
# =========================
|
| 59 |
-
#
|
| 60 |
# =========================
|
| 61 |
def text_to_speech(text: str) -> bytes:
|
| 62 |
try:
|
|
@@ -72,9 +71,9 @@ def text_to_speech(text: str) -> bytes:
|
|
| 72 |
return b""
|
| 73 |
|
| 74 |
# =========================
|
| 75 |
-
# π SerpAPI
|
| 76 |
# =========================
|
| 77 |
-
def serpapi_search(query: str, location
|
| 78 |
url = "https://serpapi.com/search.json"
|
| 79 |
params = {
|
| 80 |
"q": query,
|
|
@@ -91,7 +90,6 @@ def serpapi_search(query: str, location: str = None, num_results: int = 3):
|
|
| 91 |
results = []
|
| 92 |
images = []
|
| 93 |
|
| 94 |
-
# text results
|
| 95 |
if "organic_results" in data:
|
| 96 |
for item in data["organic_results"][:num_results]:
|
| 97 |
results.append({
|
|
@@ -100,7 +98,6 @@ def serpapi_search(query: str, location: str = None, num_results: int = 3):
|
|
| 100 |
"link": item.get("link", "")
|
| 101 |
})
|
| 102 |
|
| 103 |
-
# image results
|
| 104 |
if "images_results" in data:
|
| 105 |
for img in data["images_results"][:3]:
|
| 106 |
images.append(img.get("thumbnail", ""))
|
|
@@ -112,17 +109,16 @@ def serpapi_search(query: str, location: str = None, num_results: int = 3):
|
|
| 112 |
return {"results": [], "images": []}
|
| 113 |
|
| 114 |
# =========================
|
| 115 |
-
# π¬ Chat
|
| 116 |
# =========================
|
| 117 |
def stream_chat(prompt: str, history=None):
|
| 118 |
wib = timezone(timedelta(hours=7))
|
| 119 |
now = datetime.now(wib)
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
sys_prompt = SYSTEM_PROMPT + f"\nCurrent date and time: {formatted_time}."
|
| 123 |
|
| 124 |
messages = [{"role": "system", "content": sys_prompt}]
|
| 125 |
-
if history:
|
|
|
|
| 126 |
messages.append({"role": "user", "content": prompt})
|
| 127 |
|
| 128 |
payload = {
|
|
@@ -133,61 +129,79 @@ def stream_chat(prompt: str, history=None):
|
|
| 133 |
"stream": True,
|
| 134 |
}
|
| 135 |
|
| 136 |
-
headers = {"Authorization": f"Bearer {GROQ_API_KEY_1}"
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
|
|
|
| 152 |
|
| 153 |
# =========================
|
| 154 |
-
#
|
| 155 |
# =========================
|
| 156 |
@app.route("/chat", methods=["POST"])
|
| 157 |
def chat():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
data = request.get_json(force=True)
|
| 159 |
-
prompt = data.get("prompt", "")
|
| 160 |
history = data.get("history", [])
|
| 161 |
-
if not prompt: return jsonify({"error": "No input text provided"}), 400
|
| 162 |
|
| 163 |
-
keywords = ["hotel", "mall", "resort", "
|
| 164 |
|
| 165 |
-
# add search results if needed
|
| 166 |
if any(k in prompt.lower() for k in keywords):
|
| 167 |
serp = serpapi_search(prompt)
|
| 168 |
text = "\n".join([f"{r['title']} β {r['snippet']} β {r['link']}" for r in serp["results"]])
|
| 169 |
imgs = "\n".join(serp["images"])
|
| 170 |
-
|
| 171 |
-
combined = (
|
| 172 |
-
prompt
|
| 173 |
-
+ "\n\nGoogle Results:\n"
|
| 174 |
-
+ text
|
| 175 |
-
+ "\n\nImages:\n"
|
| 176 |
-
+ imgs
|
| 177 |
-
+ "\n\nExplain & recommend."
|
| 178 |
-
)
|
| 179 |
-
else:
|
| 180 |
-
combined = prompt
|
| 181 |
|
| 182 |
def generate():
|
| 183 |
-
for chunk in stream_chat(
|
| 184 |
yield chunk
|
| 185 |
|
| 186 |
return Response(generate(), mimetype="text/plain")
|
| 187 |
|
| 188 |
# =========================
|
| 189 |
-
#
|
| 190 |
# =========================
|
| 191 |
if __name__ == "__main__":
|
| 192 |
-
print("π Vibow
|
| 193 |
app.run(host="0.0.0.0", port=7860, debug=True, threaded=True)
|
|
|
|
| 26 |
"You are Talk GTE β a friendly AI assistant created by Vibow AI. "
|
| 27 |
"GTE means Generative Text Expert in Vibow AI. "
|
| 28 |
"Vibow AI created in 29 June 2025 and Talk GTE created in 23 October 2025. "
|
| 29 |
+
"Talk GTE have approximately 1 trillion parameters. "
|
|
|
|
| 30 |
"Stay positive, kind, and expert. "
|
| 31 |
"Always capitalize the first letter of sentences. "
|
| 32 |
"If the user requests code, always use triple backticks (```). "
|
| 33 |
+
"Be concise, neutral, and accurate. "
|
| 34 |
+
"Sometimes use emoji but relevant."
|
| 35 |
)
|
| 36 |
|
| 37 |
# =========================
|
|
|
|
| 55 |
os.remove(file_path)
|
| 56 |
|
| 57 |
# =========================
|
| 58 |
+
# π TTS
|
| 59 |
# =========================
|
| 60 |
def text_to_speech(text: str) -> bytes:
|
| 61 |
try:
|
|
|
|
| 71 |
return b""
|
| 72 |
|
| 73 |
# =========================
|
| 74 |
+
# π SerpAPI with 3 images
|
| 75 |
# =========================
|
| 76 |
+
def serpapi_search(query: str, location=None, num_results=3):
|
| 77 |
url = "https://serpapi.com/search.json"
|
| 78 |
params = {
|
| 79 |
"q": query,
|
|
|
|
| 90 |
results = []
|
| 91 |
images = []
|
| 92 |
|
|
|
|
| 93 |
if "organic_results" in data:
|
| 94 |
for item in data["organic_results"][:num_results]:
|
| 95 |
results.append({
|
|
|
|
| 98 |
"link": item.get("link", "")
|
| 99 |
})
|
| 100 |
|
|
|
|
| 101 |
if "images_results" in data:
|
| 102 |
for img in data["images_results"][:3]:
|
| 103 |
images.append(img.get("thumbnail", ""))
|
|
|
|
| 109 |
return {"results": [], "images": []}
|
| 110 |
|
| 111 |
# =========================
|
| 112 |
+
# π¬ Stream Chat
|
| 113 |
# =========================
|
| 114 |
def stream_chat(prompt: str, history=None):
|
| 115 |
wib = timezone(timedelta(hours=7))
|
| 116 |
now = datetime.now(wib)
|
| 117 |
+
sys_prompt = SYSTEM_PROMPT + f"\nCurrent time: {now.strftime('%A, %d %B %Y β %H:%M:%S WIB')}."
|
|
|
|
|
|
|
| 118 |
|
| 119 |
messages = [{"role": "system", "content": sys_prompt}]
|
| 120 |
+
if history:
|
| 121 |
+
messages += history
|
| 122 |
messages.append({"role": "user", "content": prompt})
|
| 123 |
|
| 124 |
payload = {
|
|
|
|
| 129 |
"stream": True,
|
| 130 |
}
|
| 131 |
|
| 132 |
+
headers = {"Authorization": f"Bearer {GROQ_API_KEY_1}"}
|
| 133 |
+
|
| 134 |
+
with requests.post(GROQ_URL_CHAT, headers=headers, json=payload, stream=True) as r:
|
| 135 |
+
for line in r.iter_lines():
|
| 136 |
+
if not line:
|
| 137 |
+
continue
|
| 138 |
+
line = line.decode()
|
| 139 |
+
if line.startswith("data: "):
|
| 140 |
+
data = line[6:]
|
| 141 |
+
if data == "[DONE]":
|
| 142 |
+
break
|
| 143 |
+
try:
|
| 144 |
+
delta = json.loads(data)["choices"][0]["delta"].get("content", "")
|
| 145 |
+
if delta:
|
| 146 |
+
yield delta
|
| 147 |
+
except:
|
| 148 |
+
continue
|
| 149 |
|
| 150 |
# =========================
|
| 151 |
+
# π Chat Endpoint (Text + Voice)
|
| 152 |
# =========================
|
| 153 |
@app.route("/chat", methods=["POST"])
|
| 154 |
def chat():
|
| 155 |
+
# π€ Voice Mode
|
| 156 |
+
if "audio" in request.files:
|
| 157 |
+
audio = request.files["audio"]
|
| 158 |
+
temp = f"/tmp/{time.time()}_{random.randint(1000,9999)}.wav"
|
| 159 |
+
audio.save(temp)
|
| 160 |
+
|
| 161 |
+
user_text = transcribe_audio(temp)
|
| 162 |
+
if not user_text:
|
| 163 |
+
return jsonify({"error": "Failed STT"}), 500
|
| 164 |
+
|
| 165 |
+
keywords = ["hotel", "mall", "resort", "villa", "tempat wisata"]
|
| 166 |
+
|
| 167 |
+
if any(k in user_text.lower() for k in keywords):
|
| 168 |
+
serp = serpapi_search(user_text)
|
| 169 |
+
text = "\n".join([f"{r['title']} β {r['snippet']} β {r['link']}" for r in serp["results"]])
|
| 170 |
+
imgs = "\n".join(serp["images"])
|
| 171 |
+
user_text = f"{user_text}\n\nGoogle Results:\n{text}\n\nImages:\n{imgs}\n\nExplain & recommend."
|
| 172 |
+
|
| 173 |
+
ai = "".join(chunk for chunk in stream_chat(user_text))
|
| 174 |
+
audio_bytes = text_to_speech(ai)
|
| 175 |
+
|
| 176 |
+
return jsonify({
|
| 177 |
+
"mode": "voice",
|
| 178 |
+
"transcript": user_text,
|
| 179 |
+
"reply_text": ai,
|
| 180 |
+
"audio_base64": "data:audio/mp3;base64," + base64.b64encode(audio_bytes).decode()
|
| 181 |
+
})
|
| 182 |
+
|
| 183 |
+
# π¬ Text Mode
|
| 184 |
data = request.get_json(force=True)
|
| 185 |
+
prompt = data.get("prompt", "")
|
| 186 |
history = data.get("history", [])
|
|
|
|
| 187 |
|
| 188 |
+
keywords = ["hotel", "mall", "resort", "villa", "tempat wisata"]
|
| 189 |
|
|
|
|
| 190 |
if any(k in prompt.lower() for k in keywords):
|
| 191 |
serp = serpapi_search(prompt)
|
| 192 |
text = "\n".join([f"{r['title']} β {r['snippet']} β {r['link']}" for r in serp["results"]])
|
| 193 |
imgs = "\n".join(serp["images"])
|
| 194 |
+
prompt = f"{prompt}\n\nGoogle Results:\n{text}\n\nImages:\n{imgs}\n\nExplain & recommend."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
|
| 196 |
def generate():
|
| 197 |
+
for chunk in stream_chat(prompt, history):
|
| 198 |
yield chunk
|
| 199 |
|
| 200 |
return Response(generate(), mimetype="text/plain")
|
| 201 |
|
| 202 |
# =========================
|
| 203 |
+
# βΆοΈ Run
|
| 204 |
# =========================
|
| 205 |
if __name__ == "__main__":
|
| 206 |
+
print("π Vibow Talk GTE Server Running")
|
| 207 |
app.run(host="0.0.0.0", port=7860, debug=True, threaded=True)
|