Ankitajadhav commited on
Commit
22cfb6e
1 Parent(s): 352ac63

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -8
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 gpt4all import GPT4All
 
 
 
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
- # loading gpt4all language model
49
- # load model Chat based model mistral-7b-openorca.gguf2.Q4_0.gguf
50
- # detail about gpt4all and model information - https://gpt4all.io/index.html
51
- model_name = 'Meta-Llama-3-8B-Instruct.Q4_0.gguf' # .gguf represents quantized model
52
- model_path = "gpt4all"
53
- # add path to download load the model locally, download once and load for subsequent inference
54
- model = GPT4All(model_name=model_name, model_path=model_path,device="cuda")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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