File size: 1,091 Bytes
9493940
14d4b3d
9493940
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c9dd36b
 
 
 
 
 
 
 
 
 
14d4b3d
c9dd36b
 
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

import gradio as gr
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM

def generate_text(prompt, style):
    model_name = "nomic-ai/gpt4all-13b-snoozy"
    tokenizer = AutoTokenizer.from_pretrained(model_name)
    model = AutoModelForCausalLM.from_pretrained(model_name)
    
    full_prompt = f"{prompt} Schreibe die Antwort im Stil von {style}."
    inputs = tokenizer.encode(full_prompt, return_tensors='pt')
    outputs = model.generate(inputs, max_length=150, num_return_sequences=1, no_repeat_ngram_size=2)
    
    generated = outputs[:,inputs.shape[-1]:]
    result = tokenizer.decode(generated[0], skip_special_tokens=True)
    
    return result

styles = ["eine formelle E-Mail", "eine Kurzgeschichte", "ein Gedicht", "ein wissenschaftlicher Bericht", "eine Zeitungsartikel"]

css = """
body {
  background-color: #f0f0f0;
  color: #333;
}
.gradio-input, .gradio-output {
  background-color: #fff;
  color: #333;
}
"""

iface = gr.Interface(fn=generate_text, inputs=["textbox", gr.inputs.Dropdown(choices=styles)], outputs="text", css=css)
iface.launch()