Update app.py
Browse files
app.py
CHANGED
|
@@ -1,19 +1,79 @@
|
|
| 1 |
-
|
| 2 |
import gradio as gr
|
| 3 |
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
| 4 |
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
| 4 |
|
| 5 |
+
# -------------------------
|
| 6 |
+
# CONFIGURATION
|
| 7 |
+
# -------------------------
|
| 8 |
+
# The model you want to use (must have access from Hugging Face)
|
| 9 |
+
MODEL_ID = "meta-llama/Llama-3.1-8B-Instruct"
|
| 10 |
+
|
| 11 |
+
# -------------------------
|
| 12 |
+
# TOKEN AUTHENTICATION
|
| 13 |
+
# -------------------------
|
| 14 |
+
# Your Hugging Face Access Token must be set in the HF Space as a Secret named "HF_TOKEN"
|
| 15 |
+
# To do this, go to your Hugging Face Space > Settings > Secrets > Add "HF_TOKEN"
|
| 16 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 17 |
+
|
| 18 |
+
if not HF_TOKEN:
|
| 19 |
+
raise ValueError("🚫 Hugging Face token not found. Please add 'HF_TOKEN' in your Space secrets.")
|
| 20 |
+
|
| 21 |
+
# -------------------------
|
| 22 |
+
# LOAD TOKENIZER & MODEL
|
| 23 |
+
# -------------------------
|
| 24 |
+
try:
|
| 25 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, token=HF_TOKEN)
|
| 26 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_ID, token=HF_TOKEN)
|
| 27 |
+
except Exception as e:
|
| 28 |
+
raise RuntimeError(f"🚨 Failed to load model: {e}")
|
| 29 |
|
| 30 |
+
# -------------------------
|
| 31 |
+
# CREATE PIPELINE
|
| 32 |
+
# -------------------------
|
| 33 |
+
pipe = pipeline(
|
| 34 |
+
"text-generation",
|
| 35 |
+
model=model,
|
| 36 |
+
tokenizer=tokenizer,
|
| 37 |
+
max_new_tokens=100,
|
| 38 |
+
do_sample=True,
|
| 39 |
+
temperature=0.7,
|
| 40 |
+
)
|
| 41 |
|
| 42 |
+
# -------------------------
|
| 43 |
+
# MAIN ASSISTANT FUNCTION
|
| 44 |
+
# -------------------------
|
| 45 |
+
def ai_assistant(command: str) -> str:
|
| 46 |
+
"""
|
| 47 |
+
Takes a natural language command and returns the assistant's response.
|
| 48 |
+
"""
|
| 49 |
+
prompt = (
|
| 50 |
+
"<|begin_of_text|><|start_header_id|>user<|end_header_id|>\n"
|
| 51 |
+
f"{command}"
|
| 52 |
+
"<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n"
|
| 53 |
+
)
|
| 54 |
|
| 55 |
+
try:
|
| 56 |
+
output = pipe(prompt)[0]["generated_text"]
|
| 57 |
+
# Parse only the assistant response
|
| 58 |
+
response = output.split("<|eot_id|>")[0].split("<|end_header_id|>\n")[-1].strip()
|
| 59 |
+
return response
|
| 60 |
+
except Exception as e:
|
| 61 |
+
return f"⚠️ Error: {e}"
|
| 62 |
|
| 63 |
+
# -------------------------
|
| 64 |
+
# GRADIO UI
|
| 65 |
+
# -------------------------
|
| 66 |
+
demo = gr.Interface(
|
| 67 |
+
fn=ai_assistant,
|
| 68 |
+
inputs=gr.Textbox(lines=2, placeholder="e.g. Open Chrome or Take a screenshot"),
|
| 69 |
+
outputs="text",
|
| 70 |
+
title="🧠 LLaMA 3.1 AI Assistant",
|
| 71 |
+
description="Enter a command. The AI assistant will interpret and respond like a smart OS assistant.",
|
| 72 |
+
allow_flagging="never"
|
| 73 |
+
)
|
| 74 |
|
| 75 |
+
# -------------------------
|
| 76 |
+
# LAUNCH APP
|
| 77 |
+
# -------------------------
|
| 78 |
+
if __name__ == "__main__":
|
| 79 |
+
demo.launch()
|