File size: 2,561 Bytes
5ce923a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# app.py
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM
from langchain_community.vectorstores import FAISS
from langchain_huggingface import HuggingFaceEmbeddings
import torch
import os

# Load FAISS vectorstore
embedding_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
vectorstore = FAISS.load_local("embeddings/am_index", embedding_model, allow_dangerous_deserialization=True)

# Load model
model_name = "HuggingFaceH4/zephyr-7b-beta"
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True, torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32)

# Set device
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = model.to(device)

# QA Function
def generate_answer(query, api_key):
    if api_key.strip() != "am123456":
        return "❌ Invalid API Key."

    docs = vectorstore.similarity_search(query, k=2)
    context = "\n".join([doc.page_content for doc in docs])

    prompt = f"""You are a domain expert in Additive Manufacturing.
Based on the context, answer the following question in a short and precise paragraph.

### Context:
{context}

### Question:
{query}

### Answer:
"""
    inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=1024).to(device)
    with torch.no_grad():
        outputs = model.generate(**inputs, max_new_tokens=300, temperature=0.5, top_p=0.95, do_sample=False)

    decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
    final_answer = decoded.split("### Answer:")[-1].strip()
    return final_answer

# Gradio UI
with gr.Blocks(css="""
    #main-title { width: 50%; }
    .image-class { float: right; width: 50px; margin-left: auto; }
""") as demo:
    with gr.Row():
        gr.Markdown("## 🧠 Additive Manufacturing LLM", elem_id="main-title")
        gr.Image("logo.png", elem_id="logo", show_label=False, show_download_button=False, height=50, container=False)

    gr.Markdown("Answer technical questions with a focused and clean response. No extra metadata.")

    query = gr.Textbox(label="Ask your Additive Manufacturing question")
    key = gr.Textbox(label="Enter API Key (e.g., am123456)")
    output = gr.Textbox(label="Answer")

    btn = gr.Button("Get Answer")
    btn.click(fn=generate_answer, inputs=[query, key], outputs=output)

    gr.Markdown("### πŸŽ‰ Hosted permanently on Hugging Face Spaces")

if __name__ == "__main__":
    demo.launch()