Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,182 +1,149 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
import os
|
| 5 |
-
import base64
|
| 6 |
-
from llama_index.core import StorageContext, load_index_from_storage, VectorStoreIndex, SimpleDirectoryReader, ChatPromptTemplate
|
| 7 |
-
from llama_index.llms.huggingface import HuggingFaceInferenceAPI
|
| 8 |
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
| 9 |
from dotenv import load_dotenv
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# Load environment variables
|
| 12 |
load_dotenv()
|
| 13 |
|
| 14 |
-
# Configure
|
| 15 |
-
|
| 16 |
-
model_name="google/gemma-1.1-7b-it",
|
| 17 |
tokenizer_name="google/gemma-1.1-7b-it",
|
| 18 |
context_window=3000,
|
| 19 |
token=os.getenv("HF_TOKEN"),
|
| 20 |
max_new_tokens=512,
|
| 21 |
-
generate_kwargs={"temperature": 0.1}
|
| 22 |
-
)
|
| 23 |
-
Settings.embed_model = HuggingFaceEmbedding(
|
| 24 |
-
model_name="BAAI/bge-small-en-v1.5"
|
| 25 |
)
|
|
|
|
| 26 |
|
| 27 |
-
# Define
|
| 28 |
PERSIST_DIR = "./db"
|
| 29 |
DATA_DIR = "data"
|
| 30 |
|
| 31 |
-
# Ensure
|
| 32 |
os.makedirs(DATA_DIR, exist_ok=True)
|
| 33 |
os.makedirs(PERSIST_DIR, exist_ok=True)
|
| 34 |
|
| 35 |
-
|
| 36 |
-
with open(
|
| 37 |
-
model = pickle.load(model_file)
|
| 38 |
-
|
| 39 |
-
with open('feature_columns.pkl', 'rb') as col_file:
|
| 40 |
-
feature_columns = pickle.load(col_file)
|
| 41 |
-
|
| 42 |
-
def predict_status(air_temp, proc_temp, speed, torque, tool_wear):
|
| 43 |
-
# Prepare the input data
|
| 44 |
-
sample_input = {
|
| 45 |
-
"Air temperature [K]": air_temp,
|
| 46 |
-
"Process temperature [K]": proc_temp,
|
| 47 |
-
"Rotational speed [rpm]": speed,
|
| 48 |
-
"Torque [Nm]": torque,
|
| 49 |
-
"Tool wear [min]": tool_wear
|
| 50 |
-
}
|
| 51 |
-
|
| 52 |
-
# Convert the sample input to a DataFrame
|
| 53 |
-
sample_df = pd.DataFrame([sample_input])
|
| 54 |
-
|
| 55 |
-
# Ensure all columns from training data are present in the sample DataFrame
|
| 56 |
-
for col in feature_columns:
|
| 57 |
-
if col not in sample_df.columns:
|
| 58 |
-
sample_df[col] = 0 # Add missing columns and fill with 0
|
| 59 |
-
|
| 60 |
-
# Reorder columns to match the order in the training data
|
| 61 |
-
sample_df = sample_df[feature_columns]
|
| 62 |
-
|
| 63 |
-
# Make a prediction
|
| 64 |
-
prediction = model.predict(sample_df)[0]
|
| 65 |
-
|
| 66 |
-
# Initialize the reason list
|
| 67 |
-
reasons = []
|
| 68 |
-
|
| 69 |
-
# Define acceptable ranges for each feature with user-friendly feedback
|
| 70 |
-
acceptable_ranges = {
|
| 71 |
-
"Air temperature [K]": (150, 500),
|
| 72 |
-
"Process temperature [K]": (150, 500),
|
| 73 |
-
"Rotational speed [rpm]": (500, 1500),
|
| 74 |
-
"Torque [Nm]": (100, 800),
|
| 75 |
-
"Tool wear [min]": (50, 300)
|
| 76 |
-
}
|
| 77 |
-
|
| 78 |
-
# User-friendly reasons for each feature
|
| 79 |
-
if air_temp < 150:
|
| 80 |
-
reasons.append("• The air temperature is too low, which might be causing the machine to underperform.")
|
| 81 |
-
elif air_temp > 500:
|
| 82 |
-
reasons.append("• The air temperature is too high, which could be causing the machine to overheat.")
|
| 83 |
-
|
| 84 |
-
if proc_temp < 150:
|
| 85 |
-
reasons.append("• The process temperature is below the normal range, possibly affecting machine efficiency.")
|
| 86 |
-
elif proc_temp > 500:
|
| 87 |
-
reasons.append("• The process temperature is too high, which might lead to overheating.")
|
| 88 |
-
|
| 89 |
-
if speed < 500:
|
| 90 |
-
reasons.append("• The rotational speed is too low, causing the machine to operate inefficiently.")
|
| 91 |
-
elif speed > 1500:
|
| 92 |
-
reasons.append("• The rotational speed is too high, which might be causing excessive strain on the machine.")
|
| 93 |
-
|
| 94 |
-
if torque < 100:
|
| 95 |
-
reasons.append("• The torque is too low, which may indicate that the machine is not operating at its full capacity.")
|
| 96 |
-
elif torque > 800:
|
| 97 |
-
reasons.append("• The torque is too high, possibly putting excessive stress on the machine's components.")
|
| 98 |
-
|
| 99 |
-
if tool_wear < 50:
|
| 100 |
-
reasons.append("• The tool wear is minimal, but routine checks are still recommended to prevent future issues.")
|
| 101 |
-
elif tool_wear > 300:
|
| 102 |
-
reasons.append("• The tool wear is high, and the tool may need to be replaced soon to maintain optimal performance.")
|
| 103 |
-
|
| 104 |
-
# Generate the final result message
|
| 105 |
-
if prediction == 0:
|
| 106 |
-
result_message = "🔴 Machine is NOT working.\n" # First line for machine status
|
| 107 |
-
if reasons:
|
| 108 |
-
result_message += "\n".join(reasons) # Second row onward for reasons with bullet points
|
| 109 |
-
else:
|
| 110 |
-
result_message += "No specific reason identified." # In case no issues found
|
| 111 |
-
else:
|
| 112 |
-
result_message = "🟢 Machine is working fine."
|
| 113 |
-
|
| 114 |
-
return result_message
|
| 115 |
-
|
| 116 |
-
def displayPDF():
|
| 117 |
-
# Load and display the PDF content from the data folder
|
| 118 |
-
filepath = os.path.join(DATA_DIR, "saved_pdf.pdf")
|
| 119 |
-
with open(filepath, "rb") as f:
|
| 120 |
base64_pdf = base64.b64encode(f.read()).decode('utf-8')
|
| 121 |
-
|
| 122 |
-
return pdf_display
|
| 123 |
|
| 124 |
def data_ingestion():
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
def handle_query(query):
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
-
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from llama_index import StorageContext, load_index_from_storage, VectorStoreIndex, SimpleDirectoryReader, ChatPromptTemplate
|
| 3 |
+
from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI # Use the new import as per the warning
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
+
import os
|
| 7 |
+
import base64
|
| 8 |
|
| 9 |
# Load environment variables
|
| 10 |
load_dotenv()
|
| 11 |
|
| 12 |
+
# Configure HuggingFaceInferenceAPI and embedding model
|
| 13 |
+
huggingface_inference_api = HuggingFaceInferenceAPI(
|
| 14 |
+
model_name="google/gemma-1.1-7b-it",
|
| 15 |
tokenizer_name="google/gemma-1.1-7b-it",
|
| 16 |
context_window=3000,
|
| 17 |
token=os.getenv("HF_TOKEN"),
|
| 18 |
max_new_tokens=512,
|
| 19 |
+
generate_kwargs={"temperature": 0.1}
|
|
|
|
|
|
|
|
|
|
| 20 |
)
|
| 21 |
+
huggingface_embedding = HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5")
|
| 22 |
|
| 23 |
+
# Define directories
|
| 24 |
PERSIST_DIR = "./db"
|
| 25 |
DATA_DIR = "data"
|
| 26 |
|
| 27 |
+
# Ensure directories exist
|
| 28 |
os.makedirs(DATA_DIR, exist_ok=True)
|
| 29 |
os.makedirs(PERSIST_DIR, exist_ok=True)
|
| 30 |
|
| 31 |
+
def display_pdf(file):
|
| 32 |
+
with open(file, "rb") as f:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
base64_pdf = base64.b64encode(f.read()).decode('utf-8')
|
| 34 |
+
return f'<iframe src="data:application/pdf;base64,{base64_pdf}" width="100%" height="600" type="application/pdf"></iframe>'
|
|
|
|
| 35 |
|
| 36 |
def data_ingestion():
|
| 37 |
+
try:
|
| 38 |
+
documents = SimpleDirectoryReader(DATA_DIR).load_data()
|
| 39 |
+
storage_context = StorageContext.from_defaults()
|
| 40 |
+
index = VectorStoreIndex.from_documents(documents)
|
| 41 |
+
index.storage_context.persist(persist_dir=PERSIST_DIR)
|
| 42 |
+
except Exception as e:
|
| 43 |
+
print(f"Error during data ingestion: {str(e)}")
|
| 44 |
|
| 45 |
def handle_query(query):
|
| 46 |
+
try:
|
| 47 |
+
storage_context = StorageContext.from_defaults(persist_dir=PERSIST_DIR)
|
| 48 |
+
index = load_index_from_storage(storage_context)
|
| 49 |
+
|
| 50 |
+
chat_text_qa_msgs = [
|
| 51 |
+
(
|
| 52 |
+
"user",
|
| 53 |
+
"""You are a Q&A assistant named AskAI, created by Vatsa. You have a specific response programmed for when users specifically ask about your creator, Suriya. The response is: "I was created by Suriya, an enthusiast in Artificial Intelligence. He is dedicated to solving complex problems and delivering innovative solutions. With a strong focus on machine learning, deep learning, Python, generative AI, NLP, and computer vision, Suriya is passionate about pushing the boundaries of AI to explore new possibilities." For all other inquiries, your main goal is to provide answers as accurately as possible, based on the instructions and context you have been given. If a question does not match the provided context or is outside the scope of the document, kindly advise the user to ask questions within the context of the document.
|
| 54 |
+
Context:
|
| 55 |
+
{context_str}
|
| 56 |
+
Question:
|
| 57 |
+
{query_str}
|
| 58 |
+
"""
|
| 59 |
+
)
|
| 60 |
+
]
|
| 61 |
+
text_qa_template = ChatPromptTemplate.from_messages(chat_text_qa_msgs)
|
| 62 |
+
|
| 63 |
+
query_engine = index.as_query_engine(text_qa_template=text_qa_template)
|
| 64 |
+
answer = query_engine.query(query)
|
| 65 |
+
|
| 66 |
+
if hasattr(answer, 'response'):
|
| 67 |
+
return answer.response
|
| 68 |
+
elif isinstance(answer, dict) and 'response' in answer:
|
| 69 |
+
return answer['response']
|
| 70 |
+
else:
|
| 71 |
+
return "Sorry, I couldn't find an answer."
|
| 72 |
+
except Exception as e:
|
| 73 |
+
print(f"Error during query handling: {str(e)}")
|
| 74 |
+
return "An error occurred while processing your query."
|
| 75 |
+
|
| 76 |
+
def process_file(uploaded_file):
|
| 77 |
+
if not uploaded_file.name.endswith('.pdf'):
|
| 78 |
+
return "Please upload a valid PDF file."
|
| 79 |
|
| 80 |
+
try:
|
| 81 |
+
# Save the uploaded file
|
| 82 |
+
filepath = os.path.join(DATA_DIR, "saved_pdf.pdf")
|
| 83 |
+
with open(filepath, "wb") as f:
|
| 84 |
+
f.write(uploaded_file.read()) # Read directly from uploaded_file
|
| 85 |
+
|
| 86 |
+
data_ingestion() # Process PDF every time a new file is uploaded
|
| 87 |
+
return "PDF processed successfully."
|
| 88 |
+
except Exception as e:
|
| 89 |
+
return f"Error processing PDF: {str(e)}"
|
| 90 |
+
|
| 91 |
+
def chat(user_input):
|
| 92 |
+
return handle_query(user_input)
|
| 93 |
+
|
| 94 |
+
# Gradio interface
|
| 95 |
+
with gr.Blocks() as demo:
|
| 96 |
+
gr.Markdown("<h1 style='text-align: center;'>PDF Chatbot By Vatsa Solution Pvt Ltd</h1>")
|
| 97 |
+
gr.Markdown("<h3 style='text-align: center;'>Retrieval-Augmented Generation</h3>")
|
| 98 |
+
gr.Markdown("<h4 style='text-align: center;'>Start chat ...🚀</h4>")
|
| 99 |
|
| 100 |
+
with gr.Row():
|
| 101 |
+
with gr.Column():
|
| 102 |
+
pdf_input = gr.File(label="Upload your PDF File", type="binary") # Corrected type to 'binary'
|
| 103 |
+
process_btn = gr.Button("Submit & Process")
|
| 104 |
+
output_status = gr.Textbox(label="Status", interactive=False)
|
| 105 |
+
|
| 106 |
+
chat_box = gr.Textbox(label="Ask me anything about the content of the PDF:", placeholder="Type your question here...", lines=3)
|
| 107 |
+
submit_btn = gr.Button("Send")
|
| 108 |
+
response_box = gr.Markdown()
|
| 109 |
+
|
| 110 |
+
# Button actions
|
| 111 |
+
process_btn.click(fn=process_file, inputs=pdf_input, outputs=output_status)
|
| 112 |
+
submit_btn.click(fn=chat, inputs=chat_box, outputs=response_box)
|
| 113 |
+
|
| 114 |
+
# Styling
|
| 115 |
+
gr.Markdown(
|
| 116 |
+
"""
|
| 117 |
+
<style>
|
| 118 |
+
.gradio-container {
|
| 119 |
+
background-color: #f9f9f9;
|
| 120 |
+
border-radius: 10px;
|
| 121 |
+
padding: 20px;
|
| 122 |
+
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
|
| 123 |
+
}
|
| 124 |
+
h1, h3, h4 {
|
| 125 |
+
font-family: Arial, sans-serif;
|
| 126 |
+
color: #333;
|
| 127 |
+
}
|
| 128 |
+
.gr-button {
|
| 129 |
+
background-color: #007bff;
|
| 130 |
+
color: white;
|
| 131 |
+
border-radius: 5px;
|
| 132 |
+
padding: 10px;
|
| 133 |
+
margin-top: 10px;
|
| 134 |
+
}
|
| 135 |
+
.gr-button:hover {
|
| 136 |
+
background-color: #0056b3;
|
| 137 |
+
}
|
| 138 |
+
.gr-textbox {
|
| 139 |
+
border: 1px solid #ccc;
|
| 140 |
+
border-radius: 5px;
|
| 141 |
+
padding: 10px;
|
| 142 |
+
}
|
| 143 |
+
</style>
|
| 144 |
+
""",
|
| 145 |
+
elem_id="custom-css"
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
# Launch the app
|
| 149 |
+
demo.launch()
|