Update app.py
Browse files
app.py
CHANGED
@@ -7,7 +7,7 @@ import json
|
|
7 |
import random
|
8 |
from sentence_transformers import SentenceTransformer
|
9 |
|
10 |
-
# β
Load
|
11 |
with open("texts.json", "r", encoding="utf-8") as f:
|
12 |
texts = json.load(f)
|
13 |
|
@@ -17,15 +17,15 @@ embed_model = SentenceTransformer("all-MiniLM-L6-v2")
|
|
17 |
API_KEY = os.environ.get("OPENROUTER_API_KEY")
|
18 |
MODEL = "qwen/qwen-2.5-coder-32b-instruct:free"
|
19 |
|
20 |
-
# β
Friendly
|
21 |
-
|
22 |
"Hey! Gotcha covered π",
|
23 |
"Sure thing! Hereβs what I found π§ ",
|
24 |
-
"Alright
|
25 |
"No worries β Iβve got you π",
|
26 |
"Yup, here's how it works βοΈ",
|
27 |
"Letβs dive into it π‘",
|
28 |
-
"
|
29 |
]
|
30 |
|
31 |
def get_context(query, top_k=5):
|
@@ -41,11 +41,10 @@ def chat_fn(message, history):
|
|
41 |
|
42 |
context = get_context(message)
|
43 |
|
44 |
-
system_prompt = f"""You are Codex Assistant by LogIQ Curve β a
|
45 |
-
|
46 |
-
Avoid robotic
|
47 |
|
48 |
-
Hereβs your internal knowledge:
|
49 |
{context}
|
50 |
"""
|
51 |
|
@@ -65,82 +64,24 @@ Hereβs your internal knowledge:
|
|
65 |
try:
|
66 |
response = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload)
|
67 |
response.raise_for_status()
|
68 |
-
|
69 |
-
opener = random.choice(
|
70 |
-
reply = f"{opener}\n\n{
|
71 |
except Exception as e:
|
72 |
reply = f"β Oops! Something went wrong: {e}"
|
73 |
|
74 |
return reply
|
75 |
|
76 |
-
# β
Custom CSS
|
77 |
custom_css = """
|
78 |
footer { display: none !important; }
|
79 |
button[data-testid="settings-button"] { display: none !important; }
|
80 |
-
|
81 |
-
#chatbox-wrapper {
|
82 |
-
position: fixed;
|
83 |
-
bottom: 90px;
|
84 |
-
right: 20px;
|
85 |
-
z-index: 9999;
|
86 |
-
width: 360px;
|
87 |
-
max-width: 100vw;
|
88 |
-
display: none;
|
89 |
-
border: 1px solid #ccc;
|
90 |
-
border-radius: 16px;
|
91 |
-
overflow: hidden;
|
92 |
-
box-shadow: 0 4px 20px rgba(0,0,0,0.2);
|
93 |
-
}
|
94 |
-
#chatbox-wrapper.show {
|
95 |
-
display: block;
|
96 |
-
}
|
97 |
-
#chatbot-toggle {
|
98 |
-
position: fixed;
|
99 |
-
bottom: 20px;
|
100 |
-
right: 20px;
|
101 |
-
background-color: #2a2a2a;
|
102 |
-
color: white;
|
103 |
-
border: none;
|
104 |
-
border-radius: 50%;
|
105 |
-
width: 56px;
|
106 |
-
height: 56px;
|
107 |
-
font-size: 24px;
|
108 |
-
cursor: pointer;
|
109 |
-
z-index: 10000;
|
110 |
-
}
|
111 |
-
#user-form {
|
112 |
-
padding: 16px;
|
113 |
-
display: flex;
|
114 |
-
flex-direction: column;
|
115 |
-
gap: 8px;
|
116 |
-
}
|
117 |
-
#whatsapp-btn {
|
118 |
-
margin-top: 8px;
|
119 |
-
background: #25d366;
|
120 |
-
color: white;
|
121 |
-
text-align: center;
|
122 |
-
padding: 8px;
|
123 |
-
border-radius: 8px;
|
124 |
-
text-decoration: none;
|
125 |
-
}
|
126 |
"""
|
127 |
|
|
|
128 |
with gr.Blocks(css=custom_css) as demo:
|
129 |
-
|
130 |
-
|
131 |
-
email = gr.Textbox(label="Email", placeholder="Enter your email")
|
132 |
-
phone = gr.Textbox(label="Phone", placeholder="Enter your phone")
|
133 |
-
submit_btn = gr.Button("Start Chat")
|
134 |
-
chatbot = gr.Chatbot()
|
135 |
-
msg = gr.Textbox(placeholder="Type your message...", show_label=False, visible=False)
|
136 |
-
whatsapp = gr.HTML('<a id="whatsapp-btn" href="https://wa.me/923390320120" target="_blank">π¬ Talk to Human on WhatsApp</a>')
|
137 |
-
|
138 |
-
def start_chat(n, e, p):
|
139 |
-
if n and e and p:
|
140 |
-
return gr.update(visible=True), ""
|
141 |
-
return gr.update(visible=False), "Please fill all fields to start chat."
|
142 |
-
|
143 |
-
submit_btn.click(start_chat, [name, email, phone], [msg, name])
|
144 |
|
145 |
def respond(message, history):
|
146 |
response = chat_fn(message, history)
|
@@ -149,16 +90,4 @@ with gr.Blocks(css=custom_css) as demo:
|
|
149 |
|
150 |
msg.submit(respond, [msg, chatbot], [chatbot, msg])
|
151 |
|
152 |
-
# Toggle button + JS
|
153 |
-
gr.HTML("""
|
154 |
-
<button id="chatbot-toggle">π¬</button>
|
155 |
-
<script>
|
156 |
-
const toggleBtn = document.getElementById("chatbot-toggle");
|
157 |
-
const chatbox = document.getElementById("chatbox-wrapper");
|
158 |
-
toggleBtn.onclick = () => {
|
159 |
-
chatbox.classList.toggle("show");
|
160 |
-
};
|
161 |
-
</script>
|
162 |
-
""")
|
163 |
-
|
164 |
demo.launch()
|
|
|
7 |
import random
|
8 |
from sentence_transformers import SentenceTransformer
|
9 |
|
10 |
+
# β
Load files
|
11 |
with open("texts.json", "r", encoding="utf-8") as f:
|
12 |
texts = json.load(f)
|
13 |
|
|
|
17 |
API_KEY = os.environ.get("OPENROUTER_API_KEY")
|
18 |
MODEL = "qwen/qwen-2.5-coder-32b-instruct:free"
|
19 |
|
20 |
+
# β
Friendly openers
|
21 |
+
friendly_openers = [
|
22 |
"Hey! Gotcha covered π",
|
23 |
"Sure thing! Hereβs what I found π§ ",
|
24 |
+
"Alright, let me break it down π",
|
25 |
"No worries β Iβve got you π",
|
26 |
"Yup, here's how it works βοΈ",
|
27 |
"Letβs dive into it π‘",
|
28 |
+
"Boom! Here's the scoop π₯"
|
29 |
]
|
30 |
|
31 |
def get_context(query, top_k=5):
|
|
|
41 |
|
42 |
context = get_context(message)
|
43 |
|
44 |
+
system_prompt = f"""You are Codex Assistant by LogIQ Curve β a helpful, friendly AI assistant.
|
45 |
+
Talk like you're chatting with a teammate: casual, confident, and clear. Use emojis to make things fun and engaging π.
|
46 |
+
Avoid robotic replies. NEVER mention the word "context" to the user. Just use this internally:
|
47 |
|
|
|
48 |
{context}
|
49 |
"""
|
50 |
|
|
|
64 |
try:
|
65 |
response = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload)
|
66 |
response.raise_for_status()
|
67 |
+
ai_reply = response.json()["choices"][0]["message"]["content"]
|
68 |
+
opener = random.choice(friendly_openers)
|
69 |
+
reply = f"{opener}\n\n{ai_reply}"
|
70 |
except Exception as e:
|
71 |
reply = f"β Oops! Something went wrong: {e}"
|
72 |
|
73 |
return reply
|
74 |
|
75 |
+
# β
Custom CSS to hide footer + settings
|
76 |
custom_css = """
|
77 |
footer { display: none !important; }
|
78 |
button[data-testid="settings-button"] { display: none !important; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
"""
|
80 |
|
81 |
+
# β
Gradio interface
|
82 |
with gr.Blocks(css=custom_css) as demo:
|
83 |
+
chatbot = gr.Chatbot()
|
84 |
+
msg = gr.Textbox(placeholder="Type your message...", show_label=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
|
86 |
def respond(message, history):
|
87 |
response = chat_fn(message, history)
|
|
|
90 |
|
91 |
msg.submit(respond, [msg, chatbot], [chatbot, msg])
|
92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
demo.launch()
|