Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,10 @@ sys.modules['sqlite3'] = sys.modules.pop('pysqlite3')
|
|
| 5 |
from sentence_transformers import SentenceTransformer
|
| 6 |
import chromadb
|
| 7 |
from datasets import load_dataset
|
| 8 |
-
from
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# Embedding vector
|
| 11 |
class VectorStore:
|
|
@@ -45,12 +48,31 @@ vector_store = VectorStore("embedding_vector")
|
|
| 45 |
vector_store.populate_vectors(dataset)
|
| 46 |
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
#
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
|
|
|
|
| 5 |
from sentence_transformers import SentenceTransformer
|
| 6 |
import chromadb
|
| 7 |
from datasets import load_dataset
|
| 8 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 9 |
+
import gradio as gr
|
| 10 |
+
import faiss
|
| 11 |
+
|
| 12 |
|
| 13 |
# Embedding vector
|
| 14 |
class VectorStore:
|
|
|
|
| 48 |
vector_store.populate_vectors(dataset)
|
| 49 |
|
| 50 |
|
| 51 |
+
# Load the model and tokenizer
|
| 52 |
+
# text generation model
|
| 53 |
+
model_name = "meta-llama/Meta-Llama-3-8B"
|
| 54 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 55 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 56 |
+
|
| 57 |
+
# Define the chatbot response function
|
| 58 |
+
def chatbot_response(user_input):
|
| 59 |
+
global conversation_history
|
| 60 |
+
results = vector_store.search_context(user_input, n_results=1)
|
| 61 |
+
context = results['documents'][0] if results['documents'] else ""
|
| 62 |
+
conversation_history.append(f"User: {user_input}\nContext: {context[:150]}\nBot:")
|
| 63 |
+
inputs = tokenizer("\n".join(conversation_history), return_tensors="pt")
|
| 64 |
+
outputs = model.generate(**inputs, max_length=150, do_sample=True, temperature=0.7)
|
| 65 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 66 |
+
conversation_history.append(response)
|
| 67 |
+
return response
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
# Gradio interface
|
| 71 |
+
def chat(user_input):
|
| 72 |
+
response = chatbot_response(user_input)
|
| 73 |
+
return response
|
| 74 |
+
|
| 75 |
+
iface = gr.Interface(fn=chat, inputs="text", outputs="text")
|
| 76 |
+
iface.launch()
|
| 77 |
|
| 78 |
|