Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
|
|
|
| 4 |
|
| 5 |
# ----------------------------
|
| 6 |
# Load Knowledge File
|
|
@@ -9,14 +10,14 @@ if not os.path.exists("knowledge.txt"):
|
|
| 9 |
raise FileNotFoundError("Create a 'knowledge.txt' file in this folder.")
|
| 10 |
|
| 11 |
with open("knowledge.txt", "r", encoding="utf-8") as f:
|
| 12 |
-
|
| 13 |
|
| 14 |
# ----------------------------
|
| 15 |
# Groq API Configuration
|
| 16 |
# ----------------------------
|
| 17 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY") or "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"
|
| 18 |
-
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 19 |
-
MODEL_NAME = "llama3-8b-8192"
|
| 20 |
|
| 21 |
HEADERS = {
|
| 22 |
"Authorization": f"Bearer {GROQ_API_KEY}",
|
|
@@ -24,35 +25,57 @@ HEADERS = {
|
|
| 24 |
}
|
| 25 |
|
| 26 |
# ----------------------------
|
| 27 |
-
#
|
| 28 |
# ----------------------------
|
| 29 |
-
|
| 30 |
-
"
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
"
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
# ----------------------------
|
| 42 |
-
# Chat Function
|
| 43 |
# ----------------------------
|
| 44 |
def chat(user_input, messages):
|
| 45 |
# Ensure messages is a list of dicts
|
| 46 |
if messages is None or not isinstance(messages, list):
|
| 47 |
messages = []
|
| 48 |
|
| 49 |
-
#
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
for m in messages:
|
| 52 |
if isinstance(m, dict) and "role" in m and "content" in m:
|
| 53 |
groq_messages.append({"role": m["role"], "content": m["content"]})
|
| 54 |
-
|
| 55 |
-
# Add current user input
|
| 56 |
groq_messages.append({"role": "user", "content": user_input})
|
| 57 |
|
| 58 |
payload = {
|
|
@@ -70,7 +93,7 @@ def chat(user_input, messages):
|
|
| 70 |
except Exception as e:
|
| 71 |
reply = f"Sorry, I could not process your request at the moment. ({str(e)})"
|
| 72 |
|
| 73 |
-
# Update
|
| 74 |
messages.append({"role": "user", "content": user_input})
|
| 75 |
messages.append({"role": "assistant", "content": reply})
|
| 76 |
|
|
@@ -84,13 +107,9 @@ with gr.Blocks() as demo:
|
|
| 84 |
|
| 85 |
chatbot = gr.Chatbot()
|
| 86 |
msg = gr.Textbox(label="Ask about products / Uliza kuhusu bidhaa", autofocus=True)
|
| 87 |
-
state = gr.State([])
|
| 88 |
|
| 89 |
-
msg.submit(
|
| 90 |
-
chat,
|
| 91 |
-
inputs=[msg, state],
|
| 92 |
-
outputs=[chatbot, state]
|
| 93 |
-
)
|
| 94 |
|
| 95 |
# ----------------------------
|
| 96 |
# Launch App
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
+
import re
|
| 5 |
|
| 6 |
# ----------------------------
|
| 7 |
# Load Knowledge File
|
|
|
|
| 10 |
raise FileNotFoundError("Create a 'knowledge.txt' file in this folder.")
|
| 11 |
|
| 12 |
with open("knowledge.txt", "r", encoding="utf-8") as f:
|
| 13 |
+
knowledge_lines = [line.strip() for line in f if line.strip()]
|
| 14 |
|
| 15 |
# ----------------------------
|
| 16 |
# Groq API Configuration
|
| 17 |
# ----------------------------
|
| 18 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY") or "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"
|
| 19 |
+
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 20 |
+
MODEL_NAME = "llama3-8b-8192"
|
| 21 |
|
| 22 |
HEADERS = {
|
| 23 |
"Authorization": f"Bearer {GROQ_API_KEY}",
|
|
|
|
| 25 |
}
|
| 26 |
|
| 27 |
# ----------------------------
|
| 28 |
+
# Helper: Retrieve relevant knowledge
|
| 29 |
# ----------------------------
|
| 30 |
+
def retrieve_relevant_kb(user_input, max_lines=10):
|
| 31 |
+
"""
|
| 32 |
+
Returns up to max_lines from knowledge_lines that match words in user_input.
|
| 33 |
+
Simple keyword match.
|
| 34 |
+
"""
|
| 35 |
+
user_words = set(re.findall(r"\w+", user_input.lower()))
|
| 36 |
+
relevant = []
|
| 37 |
+
|
| 38 |
+
for line in knowledge_lines:
|
| 39 |
+
line_words = set(re.findall(r"\w+", line.lower()))
|
| 40 |
+
if user_words & line_words: # intersection
|
| 41 |
+
relevant.append(line)
|
| 42 |
+
if len(relevant) >= max_lines:
|
| 43 |
+
break
|
| 44 |
+
|
| 45 |
+
# If nothing matches, fallback to first few lines
|
| 46 |
+
if not relevant:
|
| 47 |
+
relevant = knowledge_lines[:max_lines]
|
| 48 |
+
|
| 49 |
+
return "\n".join(relevant)
|
| 50 |
|
| 51 |
# ----------------------------
|
| 52 |
+
# Chat Function
|
| 53 |
# ----------------------------
|
| 54 |
def chat(user_input, messages):
|
| 55 |
# Ensure messages is a list of dicts
|
| 56 |
if messages is None or not isinstance(messages, list):
|
| 57 |
messages = []
|
| 58 |
|
| 59 |
+
# Retrieve relevant knowledge
|
| 60 |
+
kb_text = retrieve_relevant_kb(user_input, max_lines=10)
|
| 61 |
+
|
| 62 |
+
system_prompt = (
|
| 63 |
+
"You are a helpful and friendly cosmetics sales assistant. "
|
| 64 |
+
"You ONLY know the products listed below:\n\n"
|
| 65 |
+
f"{kb_text}\n\n"
|
| 66 |
+
"Rules:\n"
|
| 67 |
+
"- Only answer using the provided product list.\n"
|
| 68 |
+
"- Do NOT invent products.\n"
|
| 69 |
+
"- If user writes in Swahili, reply in Swahili.\n"
|
| 70 |
+
"- If user writes in English, reply in English.\n"
|
| 71 |
+
"- Be friendly and professional."
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
# Build Groq messages
|
| 75 |
+
groq_messages = [{"role": "system", "content": system_prompt}]
|
| 76 |
for m in messages:
|
| 77 |
if isinstance(m, dict) and "role" in m and "content" in m:
|
| 78 |
groq_messages.append({"role": m["role"], "content": m["content"]})
|
|
|
|
|
|
|
| 79 |
groq_messages.append({"role": "user", "content": user_input})
|
| 80 |
|
| 81 |
payload = {
|
|
|
|
| 93 |
except Exception as e:
|
| 94 |
reply = f"Sorry, I could not process your request at the moment. ({str(e)})"
|
| 95 |
|
| 96 |
+
# Update messages state
|
| 97 |
messages.append({"role": "user", "content": user_input})
|
| 98 |
messages.append({"role": "assistant", "content": reply})
|
| 99 |
|
|
|
|
| 107 |
|
| 108 |
chatbot = gr.Chatbot()
|
| 109 |
msg = gr.Textbox(label="Ask about products / Uliza kuhusu bidhaa", autofocus=True)
|
| 110 |
+
state = gr.State([])
|
| 111 |
|
| 112 |
+
msg.submit(chat, inputs=[msg, state], outputs=[chatbot, state])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
# ----------------------------
|
| 115 |
# Launch App
|