umairrrkhan commited on
Commit
edcd68c
·
verified ·
1 Parent(s): 18be757

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -18
app.py CHANGED
@@ -1,26 +1,63 @@
1
  import gradio as gr
2
  from transformers import AutoModelForCausalLM, AutoTokenizer
 
 
3
 
4
- # Load the model and tokenizer from Hugging Face
5
- model_name = "umairrrkhan/english-text-generation" # Replace with your actual model repo name
6
- model = AutoModelForCausalLM.from_pretrained(model_name)
7
- tokenizer = AutoTokenizer.from_pretrained(model_name)
8
 
9
- # Define a function to generate text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  def generate_text(prompt):
 
11
  inputs = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True)
12
- outputs = model.generate(
13
- inputs['input_ids'],
14
- max_length=50,
15
- attention_mask=inputs['attention_mask'],
16
- do_sample=True,
17
- temperature=0.7,
18
- top_k=50
19
- )
20
- return tokenizer.decode(outputs[0], skip_special_tokens=True)
21
-
22
- # Create a Gradio interface
23
- iface = gr.Interface(fn=generate_text, inputs="text", outputs="text")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
  # Launch the interface
26
- iface.launch()
 
 
 
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)