eddddyy commited on
Commit
46c04ea
·
verified ·
1 Parent(s): 51e64f4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -66
app.py CHANGED
@@ -1,76 +1,20 @@
1
- import os
2
- import torch
3
  import gradio as gr
4
- from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
 
5
 
6
- # -------------------------
7
- # CONFIGURATION
8
- # -------------------------
9
- MODEL_ID = "Qwen/Qwen1.5-1.8B-Chat" # small + fast for CPU
10
- HF_TOKEN = os.getenv("HF_TOKEN")
11
 
12
- if not HF_TOKEN:
13
- raise ValueError("🚫 Hugging Face token not found. Please add 'HF_TOKEN' in your Space secrets.")
14
-
15
- # -------------------------
16
- # LOAD TOKENIZER & MODEL
17
- # -------------------------
18
- try:
19
- tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, token=HF_TOKEN, trust_remote_code=True)
20
- model = AutoModelForCausalLM.from_pretrained(
21
- MODEL_ID,
22
- token=HF_TOKEN,
23
- trust_remote_code=True,
24
- device_map="cpu",
25
- torch_dtype=torch.float32,
26
- low_cpu_mem_usage=True
27
- )
28
- except Exception as e:
29
- raise RuntimeError(f"🚨 Failed to load model: {e}")
30
-
31
- # -------------------------
32
- # CREATE PIPELINE
33
- # -------------------------
34
- pipe = pipeline(
35
- "text-generation",
36
- model=model,
37
- tokenizer=tokenizer,
38
- max_new_tokens=100,
39
- do_sample=True,
40
- temperature=0.7
41
- )
42
-
43
- # -------------------------
44
- # MAIN ASSISTANT FUNCTION
45
- # -------------------------
46
- def ai_assistant(command: str) -> str:
47
- prompt = f"<|user|>\n{command}\n<|assistant|>\n"
48
-
49
- try:
50
- output = pipe(prompt)[0]["generated_text"]
51
- # Get assistant's part only
52
- if "<|assistant|>" in output:
53
- response = output.split("<|assistant|>")[-1].strip()
54
- else:
55
- response = output.strip()
56
- return response
57
- except Exception as e:
58
- return f"⚠️ Error: {e}"
59
-
60
- # -------------------------
61
- # GRADIO UI
62
- # -------------------------
63
  demo = gr.Interface(
64
- fn=ai_assistant,
65
- inputs=gr.Textbox(lines=2, placeholder="e.g. Open Chrome or Take a screenshot"),
 
 
 
66
  outputs="text",
67
- title="🧠 Qwen 1.5 AI Assistant",
68
- description="Ask anything. This assistant uses the Qwen1.5-1.8B-Chat model on CPU.",
69
  flagging_mode="never"
70
  )
71
 
72
- # -------------------------
73
- # LAUNCH APP
74
- # -------------------------
75
  if __name__ == "__main__":
76
  demo.launch()
 
 
 
1
  import gradio as gr
2
+ from model_loader import load_model
3
+ from assistant import get_assistant_response
4
 
5
+ pipe = load_model()
 
 
 
 
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  demo = gr.Interface(
8
+ fn=lambda command, execute: get_assistant_response(pipe, command, execute),
9
+ inputs=[
10
+ gr.Textbox(lines=2, placeholder="e.g. Open Chrome or Take a screenshot"),
11
+ gr.Checkbox(label="🛠️ Execute command (if possible)")
12
+ ],
13
  outputs="text",
14
+ title="🧠 Smart AI Assistant",
15
+ description="Try commands like 'Open Chrome', 'Take a screenshot', 'Create file', or 'What is AI?'",
16
  flagging_mode="never"
17
  )
18
 
 
 
 
19
  if __name__ == "__main__":
20
  demo.launch()