Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,34 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
"""
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
system_message,
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
|
20 |
-
|
21 |
-
if val[0]:
|
22 |
-
messages.append({"role": "user", "content": val[0]})
|
23 |
-
if val[1]:
|
24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
25 |
|
26 |
-
messages.append({"role": "user", "content": message})
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
for message in client.chat_completion(
|
31 |
-
messages,
|
32 |
-
max_tokens=max_tokens,
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
-
|
39 |
-
response += token
|
40 |
-
yield response
|
41 |
-
|
42 |
-
|
43 |
-
"""
|
44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
45 |
-
"""
|
46 |
demo = gr.ChatInterface(
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
-
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
-
],
|
60 |
)
|
61 |
|
62 |
-
|
63 |
if __name__ == "__main__":
|
64 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from Rag_conversation import rag_chain # Import the RAG pipeline from your script
|
3 |
+
from langchain_core.messages import HumanMessage, SystemMessage
|
4 |
|
5 |
+
# Function to process user messages
|
6 |
+
def chatbot(user_message, history):
|
7 |
+
"""
|
8 |
+
:param user_message: The latest user message
|
9 |
+
:param history: A list of (user, ai) tuples from Gradio chat
|
10 |
+
:return: (bot_reply, updated_history)
|
11 |
+
"""
|
12 |
+
chat_history = []
|
13 |
+
for h in history:
|
14 |
+
chat_history.append(HumanMessage(content=h[0])) # User message
|
15 |
+
chat_history.append(SystemMessage(content=h[1])) # AI response
|
16 |
|
17 |
+
chat_history.append(HumanMessage(content=user_message)) # Add new message
|
18 |
|
19 |
+
# Get response from RAG-based chatbot
|
20 |
+
result = rag_chain.invoke({"input": user_message, "chat_history": chat_history})
|
21 |
+
bot_reply = result["answer"]
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
+
return bot_reply
|
|
|
|
|
|
|
|
|
24 |
|
|
|
25 |
|
26 |
+
# Create Gradio UI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
demo = gr.ChatInterface(
|
28 |
+
chatbot,
|
29 |
+
title="Binghamton RAG Chatbot",
|
30 |
+
description="Ask questions about Binghamton University.",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
)
|
32 |
|
|
|
33 |
if __name__ == "__main__":
|
34 |
demo.launch()
|