Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
|
3 |
+
|
4 |
+
# Function to load the model and tokenizer
|
5 |
+
def load_model(model_name):
|
6 |
+
try:
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
9 |
+
return pipeline("text-generation", model=model, tokenizer=tokenizer)
|
10 |
+
except Exception as e:
|
11 |
+
return f"Error loading model: {str(e)}"
|
12 |
+
|
13 |
+
# Function to generate a response from the model
|
14 |
+
def chat(model_name, user_input, chat_history):
|
15 |
+
if model_name.strip() == "":
|
16 |
+
return "Please enter a valid model name.", chat_history
|
17 |
+
|
18 |
+
# Load the model
|
19 |
+
generator = load_model(model_name)
|
20 |
+
if isinstance(generator, str): # If there was an error loading the model
|
21 |
+
return generator, chat_history
|
22 |
+
|
23 |
+
# Generate a response
|
24 |
+
try:
|
25 |
+
response = generator(user_input, max_length=50, num_return_sequences=1)[0]['generated_text']
|
26 |
+
chat_history.append((user_input, response))
|
27 |
+
return "", chat_history
|
28 |
+
except Exception as e:
|
29 |
+
return f"Error generating response: {str(e)}", chat_history
|
30 |
+
|
31 |
+
# Gradio interface
|
32 |
+
with gr.Blocks() as demo:
|
33 |
+
gr.Markdown("# Chat with Any Hugging Face Model")
|
34 |
+
|
35 |
+
with gr.Row():
|
36 |
+
model_name = gr.Textbox(label="Enter Hugging Face Model Name", placeholder="e.g., gpt2, facebook/opt-125m")
|
37 |
+
|
38 |
+
chatbot = gr.Chatbot(label="Chat")
|
39 |
+
user_input = gr.Textbox(label="Your Message", placeholder="Type your message here...")
|
40 |
+
clear_button = gr.Button("Clear Chat")
|
41 |
+
|
42 |
+
# Define the chat function
|
43 |
+
user_input.submit(chat, [model_name, user_input, chatbot], [user_input, chatbot])
|
44 |
+
clear_button.click(lambda: [], None, chatbot, queue=False)
|
45 |
+
|
46 |
+
# Launch the app
|
47 |
+
demo.launch()
|