Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,52 +2,45 @@ import gradio as gr
|
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
model_name = "
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 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 |
-
footer = gr.Markdown("<p style='text-align: right; color: brown;'>Designed by Mehak Mazhar</p>")
|
| 45 |
-
|
| 46 |
-
state = gr.State([])
|
| 47 |
-
|
| 48 |
-
msg.submit(generate_response, [msg, state], [chatbot, state])
|
| 49 |
-
clear.click(lambda: ([], []), None, [chatbot, state])
|
| 50 |
-
|
| 51 |
-
# Run
|
| 52 |
if __name__ == "__main__":
|
|
|
|
| 53 |
demo.launch()
|
|
|
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
| 4 |
|
| 5 |
+
# Use a lightweight and public model
|
| 6 |
+
model_name = "distilgpt2" # You can also use "tiiuae/falcon-rw-1b" or "EleutherAI/gpt-neo-1.3B"
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 9 |
|
| 10 |
+
# Define text generation function
|
| 11 |
+
def generate_response(prompt):
|
| 12 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 13 |
+
outputs = model.generate(
|
| 14 |
+
inputs["input_ids"],
|
| 15 |
+
max_length=100,
|
| 16 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 17 |
+
do_sample=True,
|
| 18 |
+
top_k=50,
|
| 19 |
+
top_p=0.95,
|
| 20 |
+
temperature=0.7,
|
| 21 |
+
)
|
| 22 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 23 |
+
|
| 24 |
+
# Gradio interface with styling
|
| 25 |
+
def build_interface():
|
| 26 |
+
with gr.Blocks(theme=gr.themes.Base(), css="""
|
| 27 |
+
body { background-color: #FFFACD; }
|
| 28 |
+
h1 { color: brown; font-weight: bold; text-align: center; }
|
| 29 |
+
footer { text-align: center; padding-top: 10px; font-style: italic; color: #555; }
|
| 30 |
+
""") as demo:
|
| 31 |
+
gr.Markdown("# AI Text Generation Chatbot")
|
| 32 |
+
with gr.Row():
|
| 33 |
+
with gr.Column():
|
| 34 |
+
input_text = gr.Textbox(label="Enter your prompt", placeholder="e.g., Once upon a time...")
|
| 35 |
+
submit_btn = gr.Button("Generate Text")
|
| 36 |
+
with gr.Column():
|
| 37 |
+
output_text = gr.Textbox(label="Generated Text")
|
| 38 |
+
|
| 39 |
+
submit_btn.click(fn=generate_response, inputs=input_text, outputs=output_text)
|
| 40 |
+
gr.Markdown("<footer>Designed by Mehak Mazhar</footer>")
|
| 41 |
+
return demo
|
| 42 |
+
|
| 43 |
+
# Launch app
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
if __name__ == "__main__":
|
| 45 |
+
demo = build_interface()
|
| 46 |
demo.launch()
|