Spaces:
Runtime error
Runtime error
Bahodir Nematjonov
commited on
Commit
·
c2eef62
1
Parent(s):
49ff7ae
updated
Browse files- .gitignore +5 -0
- app.py +30 -27
- test.py +0 -101
.gitignore
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
.venv
|
2 |
+
__pycache__
|
3 |
+
.env
|
4 |
+
chatbot.db
|
5 |
+
.gradio
|
app.py
CHANGED
@@ -13,7 +13,6 @@ from pinecone import ServerlessSpec
|
|
13 |
from langchain.schema import Document
|
14 |
from dotenv import load_dotenv
|
15 |
from prompt import system_prompt, retriever_prompt
|
16 |
-
from test import llm
|
17 |
import os
|
18 |
import logging
|
19 |
import gradio as gr
|
@@ -170,6 +169,7 @@ prompt = ChatPromptTemplate.from_messages(
|
|
170 |
]
|
171 |
)
|
172 |
|
|
|
173 |
question_answer_chain = create_stuff_documents_chain(llm, prompt)
|
174 |
rag_chain = create_retrieval_chain(retriever, question_answer_chain)
|
175 |
|
@@ -266,7 +266,26 @@ def logout(session_id):
|
|
266 |
del base[session_id] # Clear session history
|
267 |
return None, "Logged out successfully.", None
|
268 |
|
269 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
270 |
with gr.Blocks() as demo:
|
271 |
gr.Markdown("## HumblebeeAI Customer Support Chatbot")
|
272 |
|
@@ -286,39 +305,25 @@ with gr.Blocks() as demo:
|
|
286 |
send_button = gr.Button("Send")
|
287 |
logout_button = gr.Button("Logout")
|
288 |
|
289 |
-
#
|
290 |
-
def enable_buttons(username, password):
|
291 |
-
is_valid = bool(username.strip()) and bool(password.strip())
|
292 |
-
return gr.update(interactive=is_valid), gr.update(interactive=is_valid)
|
293 |
-
|
294 |
username.change(enable_buttons, [username, password], [login_button, signup_button])
|
295 |
password.change(enable_buttons, [username, password], [login_button, signup_button])
|
296 |
|
297 |
-
#
|
298 |
-
def login_user(username, password):
|
299 |
-
session_id, message, user_id = authenticate(username, password)
|
300 |
-
if session_id:
|
301 |
-
return session_id, user_id, message, "", "", get_chat_history(user_id), gr.update(visible=True)
|
302 |
-
return None, None, message, username, password, [], gr.update(visible=False)
|
303 |
-
|
304 |
login_button.click(
|
305 |
login_user,
|
306 |
[username, password],
|
307 |
-
[session_state, user_id_state, login_status, username, password, chatbot, chat_interface]
|
308 |
)
|
309 |
|
310 |
-
#
|
311 |
-
def signup_user(username, password):
|
312 |
-
message = signup(username, password)
|
313 |
-
return message, "", ""
|
314 |
-
|
315 |
signup_button.click(
|
316 |
signup_user,
|
317 |
[username, password],
|
318 |
[login_status, username, password]
|
319 |
)
|
320 |
|
321 |
-
#
|
322 |
send_button.click(
|
323 |
get_response,
|
324 |
[msg, chatbot, session_state, user_id_state],
|
@@ -331,13 +336,11 @@ with gr.Blocks() as demo:
|
|
331 |
[msg, chatbot]
|
332 |
)
|
333 |
|
334 |
-
#
|
335 |
-
def logout_user():
|
336 |
-
return None, "", "", "", [], gr.update(visible=False)
|
337 |
-
|
338 |
logout_button.click(
|
339 |
logout_user,
|
340 |
None,
|
341 |
-
[session_state, username, password, login_status, chatbot, chat_interface]
|
342 |
)
|
343 |
-
|
|
|
|
13 |
from langchain.schema import Document
|
14 |
from dotenv import load_dotenv
|
15 |
from prompt import system_prompt, retriever_prompt
|
|
|
16 |
import os
|
17 |
import logging
|
18 |
import gradio as gr
|
|
|
169 |
]
|
170 |
)
|
171 |
|
172 |
+
llm =Ollama(model='llama3.2', base_url='http://localhost:11434')
|
173 |
question_answer_chain = create_stuff_documents_chain(llm, prompt)
|
174 |
rag_chain = create_retrieval_chain(retriever, question_answer_chain)
|
175 |
|
|
|
266 |
del base[session_id] # Clear session history
|
267 |
return None, "Logged out successfully.", None
|
268 |
|
269 |
+
import gradio as gr
|
270 |
+
|
271 |
+
def enable_buttons(username, password):
|
272 |
+
is_valid = bool(username.strip()) and bool(password.strip())
|
273 |
+
return gr.update(interactive=is_valid), gr.update(interactive=is_valid)
|
274 |
+
|
275 |
+
def login_user(username, password):
|
276 |
+
session_id, message, user_id = authenticate(username, password)
|
277 |
+
if session_id:
|
278 |
+
# Hide login, signup, and status, and show the chat interface and logout button
|
279 |
+
return session_id, user_id, message, "", "", get_chat_history(user_id), gr.update(visible=True), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)
|
280 |
+
return None, None, message, username, password, [], gr.update(visible=False), gr.update(visible=True), gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)
|
281 |
+
|
282 |
+
def signup_user(username, password):
|
283 |
+
message = signup(username, password)
|
284 |
+
return message, "", ""
|
285 |
+
|
286 |
+
def logout_user():
|
287 |
+
return None, "", "", "", [], gr.update(visible=False), gr.update(visible=True), gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)
|
288 |
+
|
289 |
with gr.Blocks() as demo:
|
290 |
gr.Markdown("## HumblebeeAI Customer Support Chatbot")
|
291 |
|
|
|
305 |
send_button = gr.Button("Send")
|
306 |
logout_button = gr.Button("Logout")
|
307 |
|
308 |
+
# Enable buttons only when both username & password are filled
|
|
|
|
|
|
|
|
|
309 |
username.change(enable_buttons, [username, password], [login_button, signup_button])
|
310 |
password.change(enable_buttons, [username, password], [login_button, signup_button])
|
311 |
|
312 |
+
# Login Logic (Clears Username & Password)
|
|
|
|
|
|
|
|
|
|
|
|
|
313 |
login_button.click(
|
314 |
login_user,
|
315 |
[username, password],
|
316 |
+
[session_state, user_id_state, login_status, username, password, chatbot, chat_interface, username, password, send_button, logout_button]
|
317 |
)
|
318 |
|
319 |
+
# Signup Logic (Clears Username & Password)
|
|
|
|
|
|
|
|
|
320 |
signup_button.click(
|
321 |
signup_user,
|
322 |
[username, password],
|
323 |
[login_status, username, password]
|
324 |
)
|
325 |
|
326 |
+
# Sending Messages
|
327 |
send_button.click(
|
328 |
get_response,
|
329 |
[msg, chatbot, session_state, user_id_state],
|
|
|
336 |
[msg, chatbot]
|
337 |
)
|
338 |
|
339 |
+
# Logout Logic (Clears Chat and Resets UI)
|
|
|
|
|
|
|
340 |
logout_button.click(
|
341 |
logout_user,
|
342 |
None,
|
343 |
+
[session_state, username, password, login_status, chatbot, chat_interface, username, password, send_button, logout_button]
|
344 |
)
|
345 |
+
|
346 |
+
demo.launch(share=True)
|
test.py
DELETED
@@ -1,101 +0,0 @@
|
|
1 |
-
import requests
|
2 |
-
from langchain.llms.base import LLM
|
3 |
-
from typing import Optional, List
|
4 |
-
from pydantic import Field
|
5 |
-
import json
|
6 |
-
|
7 |
-
|
8 |
-
class HumbleBeeLLM(LLM):
|
9 |
-
api_base: str = "https://humblebeeai-llm-host.hf.space"
|
10 |
-
username: str = Field(default=None, exclude=True) # Exclude from Pydantic validation
|
11 |
-
password: str = Field(default=None, exclude=True)
|
12 |
-
token: Optional[str] = None
|
13 |
-
|
14 |
-
def __init__(self, username: str, password: str, **kwargs):
|
15 |
-
super().__init__(**kwargs) # Initialize LangChain's LLM class properly
|
16 |
-
self.username = username
|
17 |
-
self.password = password
|
18 |
-
self.authenticate()
|
19 |
-
|
20 |
-
def authenticate(self):
|
21 |
-
"""Logs in to get the token."""
|
22 |
-
login_payload = {
|
23 |
-
"username": self.username,
|
24 |
-
"password": self.password
|
25 |
-
}
|
26 |
-
|
27 |
-
try:
|
28 |
-
response = requests.post(
|
29 |
-
f"{self.api_base}/login",
|
30 |
-
data=login_payload, # Must be 'data' instead of 'json' for form-urlencoded
|
31 |
-
headers={"Content-Type": "application/x-www-form-urlencoded"}
|
32 |
-
)
|
33 |
-
|
34 |
-
if response.status_code == 200:
|
35 |
-
self.token = response.json().get("access_token")
|
36 |
-
else:
|
37 |
-
print(f"Login failed: {response.status_code} - {response.text}")
|
38 |
-
raise Exception("Failed to authenticate.")
|
39 |
-
|
40 |
-
except requests.exceptions.RequestException as e:
|
41 |
-
raise Exception(f"Error during authentication: {str(e)}")
|
42 |
-
|
43 |
-
def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:
|
44 |
-
"""Sends a request to the LLM API and handles streaming responses."""
|
45 |
-
if not self.token:
|
46 |
-
self.authenticate()
|
47 |
-
|
48 |
-
headers = {
|
49 |
-
"Content-Type": "application/json",
|
50 |
-
"Authorization": f"Bearer {self.token}"
|
51 |
-
}
|
52 |
-
payload = {"query": prompt}
|
53 |
-
|
54 |
-
try:
|
55 |
-
response = requests.post(f"{self.api_base}/generate?stream=true", json=payload, headers=headers, stream=True)
|
56 |
-
|
57 |
-
if response.status_code == 200:
|
58 |
-
# Read the streaming response and reconstruct it token by token
|
59 |
-
full_response = ""
|
60 |
-
for line in response.iter_lines():
|
61 |
-
if line:
|
62 |
-
try:
|
63 |
-
# Clean up the streamed JSON line
|
64 |
-
data = json.loads(line.decode("utf-8").replace("data: ", ""))
|
65 |
-
token_content = data.get("content", "")
|
66 |
-
full_response += token_content
|
67 |
-
print(token_content, end="", flush=True) # Print each token as it arrives
|
68 |
-
except json.JSONDecodeError:
|
69 |
-
continue # Ignore invalid JSON lines
|
70 |
-
|
71 |
-
return full_response.strip() # Return the full response after all tokens
|
72 |
-
|
73 |
-
else:
|
74 |
-
print(f"Error {response.status_code}: {response.text}")
|
75 |
-
return f"Error {response.status_code}: {response.text}"
|
76 |
-
|
77 |
-
except requests.exceptions.RequestException as e:
|
78 |
-
return f"Request error: {str(e)}"
|
79 |
-
|
80 |
-
@property
|
81 |
-
def _identifying_params(self):
|
82 |
-
return {"api_base": self.api_base}
|
83 |
-
|
84 |
-
@property
|
85 |
-
def _llm_type(self):
|
86 |
-
return "humblebee-llm"
|
87 |
-
|
88 |
-
# ✅ Usage Example
|
89 |
-
llm = HumbleBeeLLM(username="new", password="newuser")
|
90 |
-
|
91 |
-
"""# Invoke with LangChain's API
|
92 |
-
while True:
|
93 |
-
query = input("\n...\n")
|
94 |
-
|
95 |
-
if query.lower() == 'bye': # Case insensitive check
|
96 |
-
print("Goodbye!")
|
97 |
-
break # Exit loop when 'bye' is entered
|
98 |
-
|
99 |
-
response = llm.invoke(query)
|
100 |
-
|
101 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|