File size: 2,723 Bytes
e8af0d2
a79e96b
b85adb9
4268abb
 
 
e8af0d2
4268abb
 
 
e8af0d2
a79e96b
 
 
 
b85adb9
 
e8af0d2
 
244770b
e8af0d2
244770b
 
50eae67
b85adb9
c107728
eb99cd5
244770b
50eae67
244770b
 
8455bb4
 
244770b
 
 
 
c107728
 
244770b
 
26e119b
 
 
 
 
 
e8af0d2
 
eb99cd5
c107728
 
 
26e119b
244770b
26e119b
 
 
 
 
 
 
 
 
 
 
e8af0d2
 
 
26e119b
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 streamlit as st
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
import os
from huggingface_hub import login

# Login to Hugging Face using the token
read_key = os.environ.get("Read_Token")
login(token=read_key)

# Load the tokenizer and model
tokenizer = AutoTokenizer.from_pretrained("google/gemma-2b-it")
model = AutoModelForCausalLM.from_pretrained(
    "google/gemma-2b-it",
    torch_dtype=torch.bfloat16
)

# Streamlit UI
st.title("Gemma-2B-IT Text Generator")
st.write("Enter the text and generate a structured prompt.")

# User inputs the text
text = st.text_area("Input your text", 
    "El sistema CRISPR-Cas9 permite una edición precisa del genoma mediante la creación de rupturas de doble cadena en ubicaciones específicas del ADN, lo que facilita modificaciones genéticas específicas.")

# Create the base prompt structure
base_prompt = f"""
Realiza las siguientes acciones:
1 - Tradúceme el texto delimitado por las comillas triples a los siguientes 3 idiomas: inglés, árabe y francés.
2 - Identifícame el tema principal del texto delimitado por las comillas triples.
3 - Obtén el tono en el que está escrito el texto delimitado por las comillas triples.
4 - Devuelve el resultado de todos los pasos en 1 solo JSON con los siguientes headers:TraduccionIngles, TraduccionArabe, TraduccionFrances, TemaPrincipal, Tono

Text:
```{text}```
"""

# Allow the user to modify the prompt dynamically
custom_prompt = st.text_area("Edit Prompt Template", base_prompt, height=300)

# Sliders for generation parameters
max_length = st.slider("Max Length", min_value=50, max_value=500, value=400)
temperature = st.slider("Temperature", min_value=0.1, max_value=1.5, value=0.7)
top_p = st.slider("Top-p (nucleus sampling)", min_value=0.0, max_value=1.0, value=0.9)
repetition_penalty = st.slider("Repetition Penalty", min_value=1.0, max_value=2.0, value=1.1)
do_sample = st.checkbox("Enable Sampling", value=True)

# Generate text when the button is pressed
if st.button("Generate"):
    # Correctly format the prompt using the current text input
    formatted_prompt = custom_prompt.format(text=text)

    input_ids = tokenizer(formatted_prompt, return_tensors="pt")
    
    # Generate text with specified parameters
    outputs = model.generate(
        **input_ids, 
        max_length=max_length, 
        num_return_sequences=1, 
        do_sample=do_sample, 
        temperature=temperature, 
        top_p=top_p, 
        repetition_penalty=repetition_penalty
    )
    
    generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
    
    # Display the generated text
    st.write("Generated Text:")
    st.text_area("Output", generated_text, height=300)