File size: 2,003 Bytes
bfb2b14
7cb8138
 
e0f9553
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bfb2b14
e0f9553
 
 
 
 
 
 
 
 
 
 
 
c0ddfd5
e0f9553
 
 
 
 
 
 
 
 
 
 
 
 
dd3cf39
 
 
 
 
 
 
 
 
e0f9553
dd3cf39
e0f9553
 
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
import spaces
import gradio as gr

# gr.load("models/kirankunapuli/Gemma-2B-Hinglish-LORA-v1.0").launch()

import re
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM

tokenizer = AutoTokenizer.from_pretrained("kirankunapuli/Gemma-2B-Hinglish-LORA-v1.0")
model = AutoModelForCausalLM.from_pretrained("kirankunapuli/Gemma-2B-Hinglish-LORA-v1.0")

device = "cuda:0" if torch.cuda.is_available() else "cpu"
model = model.to(device)

alpaca_prompt = """Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.

### Instruction:
{}

### Input:
{}

### Response:
{}"""


@spaces.GPU
def get_response(input_text: str) -> str:
    inputs = tokenizer(
        [
            alpaca_prompt.format(
                "Please answer the following sentence as requested",  # instruction
                input_text,  # input
                "",  # output - leave this blank for generation!
            )
        ],
        return_tensors="pt",
    ).to(device)

    outputs = model.generate(**inputs, max_new_tokens=256, use_cache=True)
    output = tokenizer.batch_decode(outputs)[0]
    response_pattern = re.compile(r"### Response:\n(.*?)<eos>", re.DOTALL)
    response_match = response_pattern.search(output)

    if response_match:
        response = response_match.group(1).strip()
        return response
    else:
        return "Response not found"


interface = gr.Interface(
    fn=get_response,
    inputs=[
        gr.Textbox(
            label="Enter your input text here",
            value="Germany ka capital city kya hai?",
            placeholder="Input to LLM",
            lines=5,
        )
    ],
    outputs=[gr.Textbox(label="LLM Output", lines=5)],
    title="Gemma Hinglish Model Inference",
    description="🤗 + 🦥 = 🔥 This model is based on google/gemma-2b and has been LoRA fine-tuned on English & Hindi language instruction datasets",
)
interface.launch()