Create Chatbot
Browse files
Chatbot
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
4 |
+
|
5 |
+
# Load pre-trained BlackMamba model and tokenizer
|
6 |
+
model_name = "blackmamba"
|
7 |
+
model = GPT2LMHeadModel.from_pretrained(model_name)
|
8 |
+
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
|
9 |
+
|
10 |
+
def generate_text(prompt, max_length=50):
|
11 |
+
inputs = tokenizer.encode(prompt, return_tensors="pt")
|
12 |
+
outputs = model.generate(inputs, max_length=max_length, num_return_sequences=1)
|
13 |
+
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
14 |
+
return generated_text
|
15 |
+
|
16 |
+
# Create Gradio interface
|
17 |
+
iface = gr.Interface(
|
18 |
+
fn=generate_text,
|
19 |
+
inputs="text",
|
20 |
+
outputs="text",
|
21 |
+
title="WormGPT",
|
22 |
+
description="An evil brother of ChatGPT for Hugging Face Gradio"
|
23 |
+
)
|
24 |
+
|
25 |
+
# Launch the interface
|
26 |
+
iface.launch()
|