sdgzero2ai commited on
Commit
a224e27
·
verified ·
1 Parent(s): 8cbe744

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -18
app.py CHANGED
@@ -1,34 +1,25 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
- # 1. Load a text-generation pipeline from Hugging Face
5
- # (Choose any model from https://huggingface.co/models)
6
  generator = pipeline(
7
  "text-generation",
8
- model="EleutherAI/gpt-neo-1.3B", # Example smaller Bloom model
9
- max_length=100
 
10
  )
11
 
12
  def chatbot(prompt):
13
- """
14
- Generate a text completion from the hugging face model pipeline.
15
- """
16
- # 2. Pass the prompt to the pipeline
17
- responses = generator(prompt, max_length=150, num_return_sequences=1)
18
- # The pipeline returns a list of dicts; we only need the generated_text
19
- generated_text = responses[0]["generated_text"]
20
-
21
- # Optionally, if you want to remove the original prompt portion from the output, you can slice it out.
22
- # For simplicity, we'll just return the entire generated text.
23
- return generated_text
24
 
25
- # 3. Create and launch the Gradio interface
26
  demo = gr.Interface(
27
  fn=chatbot,
28
  inputs="text",
29
  outputs="text",
30
- title="Hugging Face Chatbot",
31
- description="Ask anything to this Hugging Face model!"
32
  )
33
 
34
  if __name__ == "__main__":
 
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
+ # Create text-generation pipeline with explicit truncation.
 
5
  generator = pipeline(
6
  "text-generation",
7
+ model="gpt2",
8
+ max_length=100,
9
+ truncation=True # important
10
  )
11
 
12
  def chatbot(prompt):
13
+ # Generate text with the pipeline, also specifying truncation at call time (optional).
14
+ responses = generator(prompt, max_length=100, truncation=True)
15
+ return responses[0]["generated_text"]
 
 
 
 
 
 
 
 
16
 
 
17
  demo = gr.Interface(
18
  fn=chatbot,
19
  inputs="text",
20
  outputs="text",
21
+ title="Hugging Face GPT-2 Chatbot",
22
+ description="Ask anything to the GPT-2-based model!"
23
  )
24
 
25
  if __name__ == "__main__":