File size: 2,553 Bytes
dc337fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61e7052
 
dc337fb
 
 
 
61e7052
dc337fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61e7052
dc337fb
 
 
 
61e7052
dc337fb
 
 
 
 
 
 
 
 
 
 
 
61e7052
dc337fb
 
 
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
# from transformers import pipeline
# import gradio as gr


# chatbot = pipeline("text-generation", model="unsloth/DeepSeek-R1-GGUF", trust_remote_code=True)

# def chat_with_bot(user_input):
#     # Generate a response from the chatbot model
#     response = chatbot(user_input)
#     return response[0]['generated_text'] 

# interface = gr.Interface(
#     fn=chat_with_bot,  # Function to call for processing the input
#     inputs=gr.Textbox(label="Enter your message"),  # User input (text)
#     outputs=gr.Textbox(label="Chatbot Response", lines=10),  # Model output (text)
#     title="Chat with DeepSeek",  # Optional: Add a title to your interface
#     description="Chat with an AI model powered by DeepSeek!"  # Optional: Add a description
# )
# interface.launch()



from transformers import AutoModelForCausalLM, AutoTokenizer
import gradio as gr

# Load the model and tokenizer from Hugging Face
model_name = "unsloth/Llama-3.2-3B-Instruct"  # Replace with your model
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)

# Function to generate text
def generate_text(input_text, max_length=100, temperature=0.7, top_p=0.9):
    # Tokenize the input text
    inputs = tokenizer(input_text, return_tensors="pt")
    
    # Generate text using the model
    outputs = model.generate(
        inputs["input_ids"],
        max_length=max_length,
        temperature=temperature,
        top_p=top_p,
        num_return_sequences=1,
        no_repeat_ngram_size=2,
    )
    
    # Decode the generated text
    generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
    return generated_text

# Gradio Interface
def gradio_interface(input_text, max_length, temperature, top_p):
    generated_text = generate_text(input_text, max_length, temperature, top_p)
    return generated_text

# Create the Gradio app
app = gr.Interface(
    fn=gradio_interface,  # Function to call
    inputs=[
        gr.Textbox(lines=2, placeholder="Enter your prompt here...", label="Input Prompt"),
        gr.Slider(minimum=10, maximum=500, value=100, step=10, label="Max Length"),
        gr.Slider(minimum=0.1, maximum=1.0, value=0.7, step=0.1, label="Temperature"),
        gr.Slider(minimum=0.1, maximum=1.0, value=0.9, step=0.1, label="Top-p (Nucleus Sampling)"),
    ],
    outputs=gr.Textbox(lines=10, label="Generated Text"),
    title="Text Generation with Hugging Face Transformers",
    description="Generate text using a Hugging Face model.",
)

# Launch the app
app.launch()