Spaces:
Runtime error
Runtime error
Commit
Β·
5ce923a
1
Parent(s):
4f05860
Add app.py and requirements.txt for Hugging Face Space
Browse files- app.py +70 -0
- requirements.txt +6 -0
app.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
+
from langchain_community.vectorstores import FAISS
|
| 5 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
| 6 |
+
import torch
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
# Load FAISS vectorstore
|
| 10 |
+
embedding_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 11 |
+
vectorstore = FAISS.load_local("embeddings/am_index", embedding_model, allow_dangerous_deserialization=True)
|
| 12 |
+
|
| 13 |
+
# Load model
|
| 14 |
+
model_name = "HuggingFaceH4/zephyr-7b-beta"
|
| 15 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 16 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True, torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32)
|
| 17 |
+
|
| 18 |
+
# Set device
|
| 19 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 20 |
+
model = model.to(device)
|
| 21 |
+
|
| 22 |
+
# QA Function
|
| 23 |
+
def generate_answer(query, api_key):
|
| 24 |
+
if api_key.strip() != "am123456":
|
| 25 |
+
return "β Invalid API Key."
|
| 26 |
+
|
| 27 |
+
docs = vectorstore.similarity_search(query, k=2)
|
| 28 |
+
context = "\n".join([doc.page_content for doc in docs])
|
| 29 |
+
|
| 30 |
+
prompt = f"""You are a domain expert in Additive Manufacturing.
|
| 31 |
+
Based on the context, answer the following question in a short and precise paragraph.
|
| 32 |
+
|
| 33 |
+
### Context:
|
| 34 |
+
{context}
|
| 35 |
+
|
| 36 |
+
### Question:
|
| 37 |
+
{query}
|
| 38 |
+
|
| 39 |
+
### Answer:
|
| 40 |
+
"""
|
| 41 |
+
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=1024).to(device)
|
| 42 |
+
with torch.no_grad():
|
| 43 |
+
outputs = model.generate(**inputs, max_new_tokens=300, temperature=0.5, top_p=0.95, do_sample=False)
|
| 44 |
+
|
| 45 |
+
decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 46 |
+
final_answer = decoded.split("### Answer:")[-1].strip()
|
| 47 |
+
return final_answer
|
| 48 |
+
|
| 49 |
+
# Gradio UI
|
| 50 |
+
with gr.Blocks(css="""
|
| 51 |
+
#main-title { width: 50%; }
|
| 52 |
+
.image-class { float: right; width: 50px; margin-left: auto; }
|
| 53 |
+
""") as demo:
|
| 54 |
+
with gr.Row():
|
| 55 |
+
gr.Markdown("## π§ Additive Manufacturing LLM", elem_id="main-title")
|
| 56 |
+
gr.Image("logo.png", elem_id="logo", show_label=False, show_download_button=False, height=50, container=False)
|
| 57 |
+
|
| 58 |
+
gr.Markdown("Answer technical questions with a focused and clean response. No extra metadata.")
|
| 59 |
+
|
| 60 |
+
query = gr.Textbox(label="Ask your Additive Manufacturing question")
|
| 61 |
+
key = gr.Textbox(label="Enter API Key (e.g., am123456)")
|
| 62 |
+
output = gr.Textbox(label="Answer")
|
| 63 |
+
|
| 64 |
+
btn = gr.Button("Get Answer")
|
| 65 |
+
btn.click(fn=generate_answer, inputs=[query, key], outputs=output)
|
| 66 |
+
|
| 67 |
+
gr.Markdown("### π Hosted permanently on Hugging Face Spaces")
|
| 68 |
+
|
| 69 |
+
if __name__ == "__main__":
|
| 70 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
transformers
|
| 3 |
+
langchain-community
|
| 4 |
+
torch
|
| 5 |
+
faiss-cpu
|
| 6 |
+
sentence-transformers
|