Spaces:
Runtime error
Runtime error
updated app.py with jupyternotebook code.
Browse files
app.py
CHANGED
|
@@ -1,15 +1,90 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
return "Hello, " + name + "!" * int(intensity)
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
)
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
|
| 15 |
# import gradio as gr
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import json
|
| 3 |
+
from langchain.llms import GooglePalm
|
| 4 |
|
| 5 |
+
api_key = "AIzaSyCdM_aAIsW_nPbjarOF83mbX1_z1cVX2_M"
|
|
|
|
| 6 |
|
| 7 |
+
llm = GooglePalm(google_api_key = api_key, temperature=0.7)
|
| 8 |
+
|
| 9 |
+
from langchain.document_loaders.csv_loader import CSVLoader
|
| 10 |
+
|
| 11 |
+
loader = CSVLoader(file_path='aiotsmartlabs_faq.csv', source_column = 'prompt')
|
| 12 |
+
data = loader.load()
|
| 13 |
+
|
| 14 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
| 15 |
+
from langchain.vectorstores import FAISS
|
| 16 |
+
|
| 17 |
+
# instructor_embeddings = HuggingFaceEmbeddings(model_name = "Alibaba-NLP/gte-Qwen2-7B-instruct") # best model <-- but too big
|
| 18 |
+
instructor_embeddings = HuggingFaceEmbeddings(model_name = "BAAI/bge-m3")
|
| 19 |
+
# instructor_embeddings = HuggingFaceEmbeddings()
|
| 20 |
+
|
| 21 |
+
vectordb = FAISS.from_documents(documents = data, embedding = instructor_embeddings)
|
| 22 |
+
|
| 23 |
+
# e = embeddings_model.embed_query("What is your refund policy")
|
| 24 |
+
|
| 25 |
+
retriever = vectordb.as_retriever()
|
| 26 |
+
|
| 27 |
+
from langchain.prompts import PromptTemplate
|
| 28 |
+
|
| 29 |
+
prompt_template = """Given the following context and a question, generate an answer based on the context only.
|
| 30 |
+
|
| 31 |
+
In the answer try to provide as much text as possible from "response" section in the source document context without making much changes.
|
| 32 |
+
If somebody asks "Who are you?" or a similar phrase, state "I am Rishi's assistant built using a Large Language Model!"
|
| 33 |
+
If the answer is not found in the context, kindly state "I don't know. Please ask Rishi on Discord. Discord Invite Link: https://discord.gg/6ezpZGeCcM. Or email at rishi@aiotsmartlabs.com" Don't try to make up an answer.
|
| 34 |
+
|
| 35 |
+
CONTEXT: {context}
|
| 36 |
+
|
| 37 |
+
QUESTION: {question}"""
|
| 38 |
+
|
| 39 |
+
PROMPT = PromptTemplate(
|
| 40 |
+
template = prompt_template, input_variables = ["context", "question"]
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
from langchain.chains import RetrievalQA
|
| 44 |
+
|
| 45 |
+
chain = RetrievalQA.from_chain_type(llm = llm,
|
| 46 |
+
chain_type="stuff",
|
| 47 |
+
retriever=retriever,
|
| 48 |
+
input_key="query",
|
| 49 |
+
return_source_documents=True,
|
| 50 |
+
chain_type_kwargs = {"prompt": PROMPT})
|
| 51 |
+
|
| 52 |
+
# Load your LLM model and necessary components
|
| 53 |
+
# Assume `chain` is a function defined in your notebook that takes a query and returns the output as shown
|
| 54 |
+
# For this example, we'll assume the model and chain function are already available
|
| 55 |
+
|
| 56 |
+
def chatbot(query):
|
| 57 |
+
response = chain(query)
|
| 58 |
+
# Extract the 'result' part of the response
|
| 59 |
+
result = response.get('result', 'Sorry, I could not find an answer.')
|
| 60 |
+
return result
|
| 61 |
+
|
| 62 |
+
# Define the Gradio interface
|
| 63 |
+
iface = gr.Interface(
|
| 64 |
+
fn=chatbot, # Function to call
|
| 65 |
+
inputs=gr.inputs.Textbox(lines=2, placeholder="Enter your question here..."), # Input type
|
| 66 |
+
outputs="text", # Output type
|
| 67 |
+
title="Hugging Face LLM Chatbot",
|
| 68 |
+
description="Ask any question related to the documents and get an answer from the LLM model.",
|
| 69 |
)
|
| 70 |
|
| 71 |
+
# Launch the interface
|
| 72 |
+
iface.launch()
|
| 73 |
+
|
| 74 |
+
# Save this file as app.py and push it to your Hugging Face Space repository
|
| 75 |
+
|
| 76 |
+
# import gradio as gr
|
| 77 |
+
|
| 78 |
+
# def greet(name, intensity):
|
| 79 |
+
# return "Hello, " + name + "!" * int(intensity)
|
| 80 |
+
|
| 81 |
+
# demo = gr.Interface(
|
| 82 |
+
# fn=greet,
|
| 83 |
+
# inputs=["text", "slider"],
|
| 84 |
+
# outputs=["text"],
|
| 85 |
+
# )
|
| 86 |
+
|
| 87 |
+
# demo.launch()
|
| 88 |
|
| 89 |
|
| 90 |
# import gradio as gr
|