Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,21 +1,24 @@
|
|
| 1 |
|
| 2 |
import gradio as gr
|
| 3 |
import time
|
| 4 |
-
|
| 5 |
from transformers import AutoTokenizer, AutoModelForCausalLM, TextGenerationPipeline
|
| 6 |
|
| 7 |
MODEL_ID = "akshaynayaks9845/rml-ai-phi1_5-rml-100k"
|
| 8 |
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
try:
|
| 11 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 12 |
model = AutoModelForCausalLM.from_pretrained(MODEL_ID)
|
| 13 |
-
|
| 14 |
-
return pipe
|
| 15 |
except Exception as e:
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
pipe_or_err = load_pipeline()
|
| 19 |
|
| 20 |
SAMPLES = [
|
| 21 |
"What is artificial intelligence?",
|
|
@@ -25,10 +28,11 @@ SAMPLES = [
|
|
| 25 |
|
| 26 |
def generate_response(prompt, max_new_tokens=128, temperature=0.2):
|
| 27 |
start = time.time()
|
| 28 |
-
|
| 29 |
-
|
|
|
|
| 30 |
try:
|
| 31 |
-
outputs =
|
| 32 |
prompt,
|
| 33 |
max_new_tokens=int(max_new_tokens),
|
| 34 |
do_sample=bool(temperature and temperature > 0),
|
|
@@ -38,14 +42,11 @@ def generate_response(prompt, max_new_tokens=128, temperature=0.2):
|
|
| 38 |
truncation=True,
|
| 39 |
)
|
| 40 |
text = outputs[0]["generated_text"]
|
| 41 |
-
# Return only continuation if the model echoes the prompt
|
| 42 |
reply = text[len(prompt):].strip() if text.startswith(prompt) else text
|
| 43 |
elapsed = int((time.time() - start) * 1000)
|
| 44 |
-
return
|
| 45 |
-
|
| 46 |
-
(⏱️ {elapsed} ms)"
|
| 47 |
except Exception as e:
|
| 48 |
-
return
|
| 49 |
|
| 50 |
with gr.Blocks(title="RML-AI Demo") as demo:
|
| 51 |
gr.Markdown('''
|
|
|
|
| 1 |
|
| 2 |
import gradio as gr
|
| 3 |
import time
|
|
|
|
| 4 |
from transformers import AutoTokenizer, AutoModelForCausalLM, TextGenerationPipeline
|
| 5 |
|
| 6 |
MODEL_ID = "akshaynayaks9845/rml-ai-phi1_5-rml-100k"
|
| 7 |
|
| 8 |
+
_PIPE = None
|
| 9 |
+
_ERR = None
|
| 10 |
+
|
| 11 |
+
def get_pipeline():
|
| 12 |
+
global _PIPE, _ERR
|
| 13 |
+
if _PIPE is not None or _ERR is not None:
|
| 14 |
+
return _PIPE, _ERR
|
| 15 |
try:
|
| 16 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 17 |
model = AutoModelForCausalLM.from_pretrained(MODEL_ID)
|
| 18 |
+
_PIPE = TextGenerationPipeline(model=model, tokenizer=tokenizer, device=-1)
|
|
|
|
| 19 |
except Exception as e:
|
| 20 |
+
_ERR = str(e)
|
| 21 |
+
return _PIPE, _ERR
|
|
|
|
| 22 |
|
| 23 |
SAMPLES = [
|
| 24 |
"What is artificial intelligence?",
|
|
|
|
| 28 |
|
| 29 |
def generate_response(prompt, max_new_tokens=128, temperature=0.2):
|
| 30 |
start = time.time()
|
| 31 |
+
pipe, err = get_pipeline()
|
| 32 |
+
if err is not None:
|
| 33 |
+
return "Model load error: " + err
|
| 34 |
try:
|
| 35 |
+
outputs = pipe(
|
| 36 |
prompt,
|
| 37 |
max_new_tokens=int(max_new_tokens),
|
| 38 |
do_sample=bool(temperature and temperature > 0),
|
|
|
|
| 42 |
truncation=True,
|
| 43 |
)
|
| 44 |
text = outputs[0]["generated_text"]
|
|
|
|
| 45 |
reply = text[len(prompt):].strip() if text.startswith(prompt) else text
|
| 46 |
elapsed = int((time.time() - start) * 1000)
|
| 47 |
+
return reply + "\n\n(⏱️ " + str(elapsed) + " ms)"
|
|
|
|
|
|
|
| 48 |
except Exception as e:
|
| 49 |
+
return "Error: " + str(e)
|
| 50 |
|
| 51 |
with gr.Blocks(title="RML-AI Demo") as demo:
|
| 52 |
gr.Markdown('''
|