File size: 2,230 Bytes
810e1b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

import gradio as gr
import time

from transformers import AutoTokenizer, AutoModelForCausalLM, TextGenerationPipeline

MODEL_ID = "akshaynayaks9845/rml-ai-phi1_5-rml-100k"

def load_pipeline():
    try:
        tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
        model = AutoModelForCausalLM.from_pretrained(MODEL_ID)
        pipe = TextGenerationPipeline(model=model, tokenizer=tokenizer, device=-1)
        return pipe
    except Exception as e:
        return str(e)

pipe_or_err = load_pipeline()

SAMPLES = [
    "What is artificial intelligence?",
    "Explain machine learning in one sentence.",
    "What is quantum computing?",
]

def generate_response(prompt, max_new_tokens=128, temperature=0.2):
    start = time.time()
    if isinstance(pipe_or_err, str):
        return f"Model load error: {pipe_or_err}"
    try:
        outputs = pipe_or_err(
            prompt,
            max_new_tokens=int(max_new_tokens),
            do_sample=bool(temperature and temperature > 0),
            temperature=float(temperature),
            top_p=0.9,
            repetition_penalty=1.1,
            truncation=True,
        )
        text = outputs[0]["generated_text"]
        # Return only continuation if the model echoes the prompt
        reply = text[len(prompt):].strip() if text.startswith(prompt) else text
        elapsed = int((time.time() - start) * 1000)
        return f"{reply}

(⏱️ {elapsed} ms)"
    except Exception as e:
        return f"Error: {str(e)}"

with gr.Blocks(title="RML-AI Demo") as demo:
    gr.Markdown('''
    # RML-AI Demo
    Ask a question below. The model will respond in GPT-style. This is a lightweight prototype demo.
    ''')
    with gr.Row():
        prompt = gr.Textbox(label="Your question", value=SAMPLES[0])
    with gr.Row():
        max_new = gr.Slider(32, 256, value=128, step=16, label="Max new tokens")
        temp = gr.Slider(0.0, 1.0, value=0.2, step=0.1, label="Temperature")
    with gr.Row():
        btn = gr.Button("Generate")
    output = gr.Textbox(label="Answer", lines=8)
    with gr.Row():
        gr.Examples(SAMPLES, inputs=prompt)

    btn.click(generate_response, [prompt, max_new, temp], output)

if __name__ == "__main__":
    demo.launch()