Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,6 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
from langchain.document_loaders import TextLoader
|
3 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
4 |
from langchain.embeddings import HuggingFaceEmbeddings
|
@@ -39,29 +41,33 @@ qa_chain = RetrievalQA.from_chain_type(
|
|
39 |
return_source_documents=False
|
40 |
)
|
41 |
|
|
|
42 |
def preprocess_query(query):
|
43 |
if "script" in query or "code" in query.lower():
|
44 |
return f"Write a CPSL script: {query}"
|
45 |
return query
|
46 |
|
|
|
47 |
def clean_response(response):
|
48 |
result = response.get("result", "")
|
49 |
if "Answer:" in result:
|
50 |
return result.split("Answer:")[1].strip()
|
51 |
return result.strip()
|
52 |
|
|
|
53 |
def chatbot_response(user_input):
|
54 |
processed_query = preprocess_query(user_input)
|
55 |
raw_response = qa_chain.invoke({"query": processed_query})
|
56 |
return clean_response(raw_response)
|
57 |
|
58 |
-
|
59 |
with gr.Blocks() as demo:
|
60 |
gr.Markdown("# CPSL Chatbot")
|
61 |
chat_history = gr.Chatbot()
|
62 |
user_input = gr.Textbox(label="Your Message:")
|
63 |
send_button = gr.Button("Send")
|
64 |
|
|
|
65 |
def interact(user_message, history):
|
66 |
bot_reply = chatbot_response(user_message)
|
67 |
history.append((user_message, bot_reply))
|
|
|
1 |
import gradio as gr
|
2 |
+
import spaces
|
3 |
+
import torch
|
4 |
from langchain.document_loaders import TextLoader
|
5 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
6 |
from langchain.embeddings import HuggingFaceEmbeddings
|
|
|
41 |
return_source_documents=False
|
42 |
)
|
43 |
|
44 |
+
@spaces.GPU
|
45 |
def preprocess_query(query):
|
46 |
if "script" in query or "code" in query.lower():
|
47 |
return f"Write a CPSL script: {query}"
|
48 |
return query
|
49 |
|
50 |
+
@spaces.GPU
|
51 |
def clean_response(response):
|
52 |
result = response.get("result", "")
|
53 |
if "Answer:" in result:
|
54 |
return result.split("Answer:")[1].strip()
|
55 |
return result.strip()
|
56 |
|
57 |
+
@spaces.GPU
|
58 |
def chatbot_response(user_input):
|
59 |
processed_query = preprocess_query(user_input)
|
60 |
raw_response = qa_chain.invoke({"query": processed_query})
|
61 |
return clean_response(raw_response)
|
62 |
|
63 |
+
@spaces.GPU
|
64 |
with gr.Blocks() as demo:
|
65 |
gr.Markdown("# CPSL Chatbot")
|
66 |
chat_history = gr.Chatbot()
|
67 |
user_input = gr.Textbox(label="Your Message:")
|
68 |
send_button = gr.Button("Send")
|
69 |
|
70 |
+
@spaces.GPU
|
71 |
def interact(user_message, history):
|
72 |
bot_reply = chatbot_response(user_message)
|
73 |
history.append((user_message, bot_reply))
|