File size: 3,137 Bytes
8a4de8f
 
 
 
 
 
 
7fa0e76
 
8a4de8f
 
 
 
 
 
 
 
7fa0e76
 
 
 
 
 
 
 
 
8a4de8f
7fa0e76
 
 
 
 
 
 
8a4de8f
 
 
 
 
 
 
 
7fa0e76
 
 
 
8a4de8f
 
 
7fa0e76
8a4de8f
 
 
 
 
 
7fa0e76
 
2949d74
8a4de8f
 
 
 
 
 
 
 
7fa0e76
8a4de8f
 
 
 
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
71
72
73
import gradio as gr
from ctransformers import AutoModelForCausalLM
from transformers import AutoTokenizer, pipeline
import torch
import re

# Initialize the model
model = AutoModelForCausalLM.from_pretrained("Detsutut/Igea-1B-instruct-GGUF-Q4", model_file="unsloth.Q4_K_M.gguf", model_type="mistral", hf=True)
tokenizer = AutoTokenizer.from_pretrained( "Detsutut/Igea-1B-instruct")


gen_pipeline = pipeline(
    "text-generation",
    model=model,
    tokenizer=tokenizer
)

alpaca_instruct_prompt = """
{}

### Istruzione:
{}

### Risposta:
{}"""

# Define the function to generate text
def generate_text(input_text, max_new_tokens=30, temperature=1, top_p=0.95):

    prompt = alpaca_instruct_prompt.format("Di seguito è riportata un'istruzione che descrive un compito. Scrivi una risposta che completi in modo appropriato la richiesta.", 
                                           input_text, 
                                           ""
    )
    
    output = gen_pipeline(
        input_text,
        max_new_tokens=max_new_tokens,
        temperature=temperature,
        top_p=top_p,
        return_full_text = False
    )
    generated_text = output[0]['generated_text']
    
    if generated_text[-1] not in [".","!","?","\n"]:
        generated_text = generated_text + " [...]"
    
    return f"<span>{input_text}</span><b style='color: blue;'>{generated_text}</b>"

# Create the Gradio interface
input_text = gr.Textbox(lines=2, placeholder="Enter your request here...", label="Input Text")

max_new_tokens = gr.Slider(minimum=1, maximum=200, value=30, step=1, label="Max New Tokens")
temperature = gr.Slider(minimum=0.1, maximum=2.0, value=1.0, step=0.1, label="Temperature")
top_p = gr.Slider(minimum=0.0, maximum=1.0, value=0.95, step=0.01, label="Top-p")

with gr.Blocks(css="#outbox { border-radius: 8px !important; border: 1px solid #e5e7eb !important; padding: 8px !important; text-align:center !important;}") as iface:
    gr.Markdown("# Igea Instruct Interface ⚕️🩺")
    gr.Markdown("🐢💬 To guarantee a reasonable througput (<1 min to answer with default settings), this space employs a **GGUF quantized version of [Igea 1B](https://huggingface.co/bmi-labmedinfo/Igea-1B-v0.0.1)**, optimized for **hardware-limited, CPU-only machines** like the free-tier HuggingFace space. Quantized models may result in significant performance degradation and therefore are not representative of the original model capabilities.")
    gr.Markdown("⚠️ Read the **[bias, risks and limitations](https://huggingface.co/bmi-labmedinfo/Igea-1B-v0.0.1#%F0%9F%9A%A8%E2%9A%A0%EF%B8%8F%F0%9F%9A%A8-bias-risks-and-limitations-%F0%9F%9A%A8%E2%9A%A0%EF%B8%8F%F0%9F%9A%A8)** of Igea before use!")
    input_text.render()
    with gr.Accordion("Advanced Options", open=False):
        max_new_tokens.render()
        temperature.render()
        top_p.render()
    output = gr.HTML(label="Generated Text",elem_id="outbox")

    btn = gr.Button("Generate")
    btn.click(generate_text, [input_text, max_new_tokens, temperature, top_p], output)

# Launch the interface
if __name__ == "__main__":
    iface.launch(inline=True)