Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import GPT2Tokenizer, GPT2LMHeadModel
|
4 |
+
|
5 |
+
# Load the language model and tokenizer
|
6 |
+
tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
|
7 |
+
model = GPT2LMHeadModel.from_pretrained('gpt2')
|
8 |
+
|
9 |
+
# Define the coding mode function
|
10 |
+
def coding_mode(text):
|
11 |
+
# Prepend the text with a coding prompt
|
12 |
+
prompt = ">>> "
|
13 |
+
input_text = prompt + text
|
14 |
+
# Generate a response from the model
|
15 |
+
input_ids = tokenizer.encode(input_text, return_tensors="pt")
|
16 |
+
output_ids = model.generate(input_ids, max_length=1000, do_sample=True)
|
17 |
+
response = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
18 |
+
# Remove the coding prompt from the response
|
19 |
+
response = response[len(prompt):]
|
20 |
+
return response
|
21 |
+
|
22 |
+
# Define the normal mode function
|
23 |
+
def normal_mode(text):
|
24 |
+
# Generate a response from the model
|
25 |
+
input_ids = tokenizer.encode(text, return_tensors="pt")
|
26 |
+
output_ids = model.generate(input_ids, max_length=1000, do_sample=True)
|
27 |
+
response = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
28 |
+
return response
|
29 |
+
|
30 |
+
# Create the Gradio interface
|
31 |
+
iface = gr.Interface(
|
32 |
+
fn=normal_mode,
|
33 |
+
inputs=gr.inputs.Chat(
|
34 |
+
placeholder="Enter your message here...",
|
35 |
+
prompt="Me: ",
|
36 |
+
allow_audio_input=False,
|
37 |
+
allow_video_input=False,
|
38 |
+
),
|
39 |
+
outputs=gr.outputs.Textbox(placeholder="Output text will appear here..."),
|
40 |
+
title="Chatbot",
|
41 |
+
description="Enter text and the chatbot will respond!",
|
42 |
+
theme="compact",
|
43 |
+
layout="vertical",
|
44 |
+
allow_flagging=False,
|
45 |
+
allow_screenshot=False,
|
46 |
+
allow_sharing=False,
|
47 |
+
examples=[
|
48 |
+
["Hi there!"],
|
49 |
+
["What's your name?"],
|
50 |
+
["How old are you?"],
|
51 |
+
["What do you like to do for fun?"],
|
52 |
+
],
|
53 |
+
)
|
54 |
+
|
55 |
+
# Add the coding mode toggle to the interface
|
56 |
+
iface.add_mode("Coding Mode", coding_mode)
|
57 |
+
|
58 |
+
# Launch the interface
|
59 |
+
iface.launch(share=True)
|