rohith-yarramala commited on
Commit
c76f995
·
verified ·
1 Parent(s): 4d88ddc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -0
app.py CHANGED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import gradio as gr
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
4
+
5
+ # --- ✅ Load Model & Tokenizer ---
6
+ MODEL_PATH = "rohith-yarramala/asyncapi-assistant-model-merged"
7
+
8
+ # 🚨 Force CPU mode (NO bitsandbytes, NO quantization)
9
+ device = "cpu"
10
+
11
+ model = AutoModelForCausalLM.from_pretrained(
12
+ MODEL_PATH,
13
+ torch_dtype=torch.float32, # ✅ Force CPU-friendly dtype
14
+ device_map=device, # ✅ Ensure model is loaded on CPU
15
+ trust_remote_code=True, # ✅ Required for custom model code
16
+ low_cpu_mem_usage=True # ✅ Reduce CPU memory footprint
17
+ )
18
+
19
+ # Load tokenizer
20
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH, trust_remote_code=True)
21
+ model.config.pad_token_id = tokenizer.eos_token_id # ✅ Avoid generation warnings
22
+
23
+ print("✅ Model and tokenizer loaded successfully!")
24
+
25
+ # --- 🚀 Define Chatbot Function ---
26
+ def asyncapi_chatbot(question):
27
+ inputs = tokenizer(question, return_tensors="pt").to(device)
28
+ output = model.generate(**inputs, max_length=300, use_cache=False)
29
+ return tokenizer.decode(output[0], skip_special_tokens=True)
30
+
31
+ # --- 🎨 Gradio UI ---
32
+ css = """
33
+ h1 { text-align: center; font-size: 28px; color: #4CAF50; }
34
+ textarea { font-size: 16px; }
35
+ """
36
+
37
+ iface = gr.Interface(
38
+ fn=asyncapi_chatbot,
39
+ inputs=gr.Textbox(label="Ask an AsyncAPI Question", placeholder="What is an AsyncAPI schema?"),
40
+ outputs=gr.Textbox(label="AI Response"),
41
+ title="AsyncAPI Assistant 🤖",
42
+ description="Ask any question about AsyncAPI, event-driven architecture, or message brokers.",
43
+ theme="compact",
44
+ allow_flagging="never",
45
+ css=css
46
+ )
47
+
48
+ # --- 🔥 Launch in Public Mode ---
49
+ iface.launch()