Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,62 +1,31 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
|
| 4 |
-
from langchain.embeddings import HuggingFaceEmbeddings
|
| 5 |
-
from langchain.vectorstores import FAISS
|
| 6 |
-
from langchain.llms import HuggingFacePipeline
|
| 7 |
-
from langchain.chains import RetrievalQA
|
| 8 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
docs = doc_loader.load()
|
| 13 |
-
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
|
| 14 |
-
split_docs = text_splitter.split_documents(docs)
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", torch_dtype="auto")
|
| 24 |
-
qa_pipeline = pipeline(
|
| 25 |
-
"text-generation",
|
| 26 |
-
model=model,
|
| 27 |
-
tokenizer=tokenizer,
|
| 28 |
-
max_new_tokens=500,
|
| 29 |
-
pad_token_id=tokenizer.eos_token_id
|
| 30 |
-
)
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
retriever = vectordb.as_retriever(search_kwargs={"k": 5})
|
| 35 |
-
qa_chain = RetrievalQA.from_chain_type(
|
| 36 |
-
retriever=retriever,
|
| 37 |
-
chain_type="stuff",
|
| 38 |
-
llm=llm,
|
| 39 |
-
return_source_documents=False
|
| 40 |
-
)
|
| 41 |
|
| 42 |
-
|
| 43 |
-
if "script" in query or "code" in query.lower():
|
| 44 |
-
return f"Write a CPSL script: {query}"
|
| 45 |
-
return query
|
| 46 |
|
| 47 |
-
|
| 48 |
-
result = response.get("result", "")
|
| 49 |
-
if "Answer:" in result:
|
| 50 |
-
return result.split("Answer:")[1].strip()
|
| 51 |
-
return result.strip()
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
return clean_response(raw_response)
|
| 57 |
|
| 58 |
-
# Gradio interface
|
| 59 |
-
with gr.Blocks() as
|
| 60 |
gr.Markdown("# CPSL Chatbot")
|
| 61 |
chat_history = gr.Chatbot()
|
| 62 |
user_input = gr.Textbox(label="Your Message:")
|
|
@@ -67,4 +36,8 @@ with gr.Blocks() as demo:
|
|
| 67 |
history.append((user_message, bot_reply))
|
| 68 |
return history, history
|
| 69 |
|
| 70 |
-
send_button.click(interact, inputs=[user_input, chat_history], outputs=[chat_history, chat_history])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from huggingface_hub import InferenceClient
|
| 3 |
+
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# Use the Hugging Face Inference API
|
| 6 |
+
client = InferenceClient("01-ai/Yi-Coder-9B-Chat")
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# Load the dataset
|
| 9 |
+
with open("dataset.txt", "r", encoding='utf-8') as f:
|
| 10 |
+
dataset = f.read()
|
| 11 |
|
| 12 |
+
def chatbot_response(user_input):
|
| 13 |
+
# Combine the dataset and user input
|
| 14 |
+
prompt = f"""Based on the following dataset, answer the user's question:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
Dataset:
|
| 17 |
+
{dataset}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
User question: {user_input}
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
Answer:"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
# Use the Inference API to generate a response
|
| 24 |
+
response = client.text_generation(prompt, max_new_tokens=500, temperature=0.7)
|
| 25 |
+
return response
|
|
|
|
| 26 |
|
| 27 |
+
# Set up the Gradio interface
|
| 28 |
+
with gr.Blocks() as chat_interface:
|
| 29 |
gr.Markdown("# CPSL Chatbot")
|
| 30 |
chat_history = gr.Chatbot()
|
| 31 |
user_input = gr.Textbox(label="Your Message:")
|
|
|
|
| 36 |
history.append((user_message, bot_reply))
|
| 37 |
return history, history
|
| 38 |
|
| 39 |
+
send_button.click(interact, inputs=[user_input, chat_history], outputs=[chat_history, chat_history])
|
| 40 |
+
|
| 41 |
+
# Launch the interface
|
| 42 |
+
if __name__ == "__main__":
|
| 43 |
+
chat_interface.launch()
|