Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,26 +1,63 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
|
|
3 |
|
4 |
-
#
|
5 |
-
|
6 |
-
|
7 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
|
9 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
def generate_text(prompt):
|
|
|
11 |
inputs = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True)
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
# Launch the interface
|
26 |
-
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
import torch
|
4 |
+
import logging
|
5 |
|
6 |
+
# Configure logging
|
7 |
+
logging.basicConfig(level=logging.INFO)
|
8 |
+
logger = logging.getLogger(__name__)
|
|
|
9 |
|
10 |
+
# Model and tokenizer setup
|
11 |
+
def setup_model_and_tokenizer():
|
12 |
+
logger.info("Loading model and tokenizer...")
|
13 |
+
model_name = "umairrrkhan/english-text-generation" # Replace with your model
|
14 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
15 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
16 |
+
|
17 |
+
# Ensure pad_token is set
|
18 |
+
if tokenizer.pad_token is None:
|
19 |
+
tokenizer.pad_token = tokenizer.eos_token
|
20 |
+
if model.config.pad_token_id is None:
|
21 |
+
model.config.pad_token_id = tokenizer.pad_token_id
|
22 |
+
|
23 |
+
logger.info("Model and tokenizer loaded successfully.")
|
24 |
+
return model, tokenizer
|
25 |
+
|
26 |
+
model, tokenizer = setup_model_and_tokenizer()
|
27 |
+
|
28 |
+
# Define text generation function
|
29 |
def generate_text(prompt):
|
30 |
+
logger.info(f"Received prompt: {prompt}")
|
31 |
inputs = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True)
|
32 |
+
|
33 |
+
try:
|
34 |
+
logger.info("Generating text...")
|
35 |
+
outputs = model.generate(
|
36 |
+
inputs['input_ids'],
|
37 |
+
max_length=50,
|
38 |
+
attention_mask=inputs['attention_mask'],
|
39 |
+
do_sample=True,
|
40 |
+
temperature=0.7,
|
41 |
+
top_k=50,
|
42 |
+
)
|
43 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
44 |
+
logger.info(f"Generated response: {response}")
|
45 |
+
return response
|
46 |
+
except Exception as e:
|
47 |
+
logger.error(f"Error during text generation: {e}")
|
48 |
+
return "An error occurred during text generation."
|
49 |
+
|
50 |
+
# Create Gradio interface
|
51 |
+
iface = gr.Interface(
|
52 |
+
fn=generate_text,
|
53 |
+
inputs="text",
|
54 |
+
outputs="text",
|
55 |
+
title="AI Text Generation Chatbot",
|
56 |
+
description="Type a prompt and see what the AI generates!",
|
57 |
+
examples=["Tell me a story about a robot.", "Write a poem about the moon."]
|
58 |
+
)
|
59 |
|
60 |
# Launch the interface
|
61 |
+
if __name__ == "__main__":
|
62 |
+
logger.info("Launching Gradio interface...")
|
63 |
+
iface.launch(debug=True, server_name="0.0.0.0", server_port=7860)
|