File size: 1,532 Bytes
b259079
d8ce1f0
 
0ea7176
 
d8ce1f0
0ea7176
b259079
0ea7176
 
 
b259079
9bac514
0ea7176
 
 
 
 
 
 
 
9bac514
0ea7176
 
 
 
d8ce1f0
0ea7176
 
 
 
 
 
 
d8ce1f0
0ea7176
 
 
 
 
9bac514
0ea7176
 
 
 
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
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM

# Load the tokenizer and model
print("Loading tokenizer...")
tokenizer = AutoTokenizer.from_pretrained("kolbeins/model")
print("Tokenizer loaded.")

print("Loading model...")
model = AutoModelForCausalLM.from_pretrained("kolbeins/model")
print("Model loaded.")

def chat(input_txt):
    """
    Function to generate a response using the model for the given input text.
    """
    try:
        print("Tokenizing input...")
        # Tokenizing the input text, making sure to add special tokens if necessary
        inputs = tokenizer(input_txt, return_tensors="pt", padding=True, truncation=True, max_length=512)
        print(f"Tokenized inputs: {inputs}")

        print("Generating output...")
        # Generate the output using the model
        outputs = model.generate(**inputs)
        print(f"Generated output: {outputs}")

        print("Decoding output...")
        # Decode the output (the model generates token IDs, so we need to decode them back to text)
        response = tokenizer.decode(outputs[0], skip_special_tokens=True)
        print(f"Decoded response: {response}")
        
        # Return the generated response
        return response

    except Exception as e:
        print(f"Error during inference: {e}")
        return f"Error: {e}"

# Define the Gradio interface for the chatbot
demo = gr.Interface(fn=chat, inputs="text", outputs="text")

# Launch the interface
print("Launching Gradio interface...")
demo.launch()