Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
def load_pipeline():
|
| 10 |
+
try:
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 12 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_ID)
|
| 13 |
+
pipe = TextGenerationPipeline(model=model, tokenizer=tokenizer, device=-1)
|
| 14 |
+
return pipe
|
| 15 |
+
except Exception as e:
|
| 16 |
+
return str(e)
|
| 17 |
+
|
| 18 |
+
pipe_or_err = load_pipeline()
|
| 19 |
+
|
| 20 |
+
SAMPLES = [
|
| 21 |
+
"What is artificial intelligence?",
|
| 22 |
+
"Explain machine learning in one sentence.",
|
| 23 |
+
"What is quantum computing?",
|
| 24 |
+
]
|
| 25 |
+
|
| 26 |
+
def generate_response(prompt, max_new_tokens=128, temperature=0.2):
|
| 27 |
+
start = time.time()
|
| 28 |
+
if isinstance(pipe_or_err, str):
|
| 29 |
+
return f"Model load error: {pipe_or_err}"
|
| 30 |
+
try:
|
| 31 |
+
outputs = pipe_or_err(
|
| 32 |
+
prompt,
|
| 33 |
+
max_new_tokens=int(max_new_tokens),
|
| 34 |
+
do_sample=bool(temperature and temperature > 0),
|
| 35 |
+
temperature=float(temperature),
|
| 36 |
+
top_p=0.9,
|
| 37 |
+
repetition_penalty=1.1,
|
| 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 f"{reply}
|
| 45 |
+
|
| 46 |
+
(⏱️ {elapsed} ms)"
|
| 47 |
+
except Exception as e:
|
| 48 |
+
return f"Error: {str(e)}"
|
| 49 |
+
|
| 50 |
+
with gr.Blocks(title="RML-AI Demo") as demo:
|
| 51 |
+
gr.Markdown('''
|
| 52 |
+
# RML-AI Demo
|
| 53 |
+
Ask a question below. The model will respond in GPT-style. This is a lightweight prototype demo.
|
| 54 |
+
''')
|
| 55 |
+
with gr.Row():
|
| 56 |
+
prompt = gr.Textbox(label="Your question", value=SAMPLES[0])
|
| 57 |
+
with gr.Row():
|
| 58 |
+
max_new = gr.Slider(32, 256, value=128, step=16, label="Max new tokens")
|
| 59 |
+
temp = gr.Slider(0.0, 1.0, value=0.2, step=0.1, label="Temperature")
|
| 60 |
+
with gr.Row():
|
| 61 |
+
btn = gr.Button("Generate")
|
| 62 |
+
output = gr.Textbox(label="Answer", lines=8)
|
| 63 |
+
with gr.Row():
|
| 64 |
+
gr.Examples(SAMPLES, inputs=prompt)
|
| 65 |
+
|
| 66 |
+
btn.click(generate_response, [prompt, max_new, temp], output)
|
| 67 |
+
|
| 68 |
+
if __name__ == "__main__":
|
| 69 |
+
demo.launch()
|