Deva1211 commited on
Commit
f0d1216
·
1 Parent(s): 2d07c07

api error 4

Browse files
Files changed (4) hide show
  1. .gitignore +42 -0
  2. README.md +44 -5
  3. app.py +78 -45
  4. requirements.txt +3 -3
.gitignore ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Python
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+ *.so
6
+ .Python
7
+ build/
8
+ develop-eggs/
9
+ dist/
10
+ downloads/
11
+ eggs/
12
+ .eggs/
13
+ lib/
14
+ lib64/
15
+ parts/
16
+ sdist/
17
+ var/
18
+ wheels/
19
+ *.egg-info/
20
+ .installed.cfg
21
+ *.egg
22
+
23
+ # Virtual environments
24
+ venv/
25
+ env/
26
+ ENV/
27
+
28
+ # IDEs
29
+ .vscode/
30
+ .idea/
31
+ *.swp
32
+ *.swo
33
+
34
+ # OS
35
+ .DS_Store
36
+ Thumbs.db
37
+
38
+ # Gradio
39
+ flagged/
40
+
41
+ # Model cache (optional - remove if you want to cache models)
42
+ # .cache/
README.md CHANGED
@@ -1,12 +1,51 @@
1
  ---
2
- title: Chatbot
3
- emoji: 💬
4
- colorFrom: yellow
5
  colorTo: purple
6
  sdk: gradio
7
- sdk_version: 5.0.1
8
  app_file: app.py
9
  pinned: false
 
10
  ---
11
 
12
- An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: DialoGPT Chatbot
3
+ emoji: 🤖
4
+ colorFrom: blue
5
  colorTo: purple
6
  sdk: gradio
7
+ sdk_version: 3.50.2
8
  app_file: app.py
9
  pinned: false
10
+ license: mit
11
  ---
12
 
13
+ # DialoGPT Chatbot
14
+
15
+ A conversational AI chatbot powered by Microsoft's DialoGPT-medium model, hosted on Hugging Face Spaces.
16
+
17
+ ## About
18
+
19
+ This chatbot uses the `microsoft/DialoGPT-medium` model, a pre-trained conversational AI model that can engage in natural dialogue. The interface is built with Gradio for easy interaction.
20
+
21
+ ## Features
22
+
23
+ - Natural conversation flow
24
+ - Context-aware responses based on chat history
25
+ - Clean and user-friendly interface
26
+ - Example prompts to get started
27
+ - Clear chat functionality
28
+
29
+ ## Usage
30
+
31
+ Simply type your message in the text box and press Enter to chat with the bot. The conversation history is maintained throughout the session.
32
+
33
+ ## Technical Details
34
+
35
+ - **Model**: microsoft/DialoGPT-medium
36
+ - **Framework**: PyTorch + Transformers
37
+ - **Interface**: Gradio 3.50.2
38
+ - **Hosting**: Hugging Face Spaces (CPU)
39
+
40
+ ## Installation
41
+
42
+ If you want to run this locally:
43
+
44
+ ```bash
45
+ pip install torch==2.1.0 transformers==4.35.2 gradio==3.50.2
46
+ python app.py
47
+ ```
48
+
49
+ ## License
50
+
51
+ MIT License
app.py CHANGED
@@ -1,59 +1,92 @@
1
- # app.py
2
-
3
  import gradio as gr
4
  import torch
5
  from transformers import AutoModelForCausalLM, AutoTokenizer
6
 
7
- print("Loading DialoGPT-medium model...")
8
-
9
- # Load the tokenizer and model
10
  tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
11
  model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
12
 
13
- print("Model loaded successfully!")
 
 
14
 
15
- # Define the prediction function
16
- def predict(message, history):
17
- # Format the history for DialoGPT.
18
- # It expects a flat string of alternating user/bot messages separated by an end-of-string token.
19
- history_transformer_format = ""
20
- for user_msg, bot_msg in history:
21
- history_transformer_format += user_msg + tokenizer.eos_token
22
- history_transformer_format += bot_msg + tokenizer.eos_token
23
-
24
- # Append the new user message
25
- history_transformer_format += message + tokenizer.eos_token
26
 
27
- # Tokenize the input
28
- new_user_input_ids = tokenizer.encode(history_transformer_format, return_tensors='pt')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
- # Generate a response
31
- bot_output_ids = model.generate(
32
- new_user_input_ids,
33
- max_length=1250,
34
- pad_token_id=tokenizer.eos_token_id,
35
- no_repeat_ngram_size=3,
36
- do_sample=True,
37
- top_k=100,
38
- top_p=0.7,
39
- temperature=0.8
 
 
 
 
 
 
 
 
 
 
 
40
  )
41
-
42
- # Decode the response, skipping the input part to avoid repetition
43
- response = tokenizer.decode(bot_output_ids[:, new_user_input_ids.shape[-1]:][0], skip_special_tokens=True)
44
 
45
- return response
46
-
47
- # Build the Gradio interface using gr.ChatInterface
48
- # This component is simpler and automatically includes undo and clear buttons.
49
- demo = gr.ChatInterface(
50
- fn=predict,
51
- title="DialoGPT Chatbot 🤖",
52
- description="Ask me anything! This chatbot is running the microsoft/DialoGPT-medium model.",
53
- theme="soft",
54
- examples=["Hello!", "How does a computer work?", "Tell me a joke about AI."]
55
- )
56
 
57
- # Launch the app. .queue() is recommended for handling multiple users.
58
  if __name__ == "__main__":
59
- demo.queue().launch()
 
 
 
 
1
  import gradio as gr
2
  import torch
3
  from transformers import AutoModelForCausalLM, AutoTokenizer
4
 
5
+ # Load model and tokenizer
6
+ print("Loading DialoGPT-medium...")
 
7
  tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
8
  model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
9
 
10
+ # Add pad token if it doesn't exist
11
+ if tokenizer.pad_token is None:
12
+ tokenizer.pad_token = tokenizer.eos_token
13
 
14
+ print("Model loaded successfully!")
 
 
 
 
 
 
 
 
 
 
15
 
16
+ def respond(message, history):
17
+ """Generate response for the chatbot"""
18
+ try:
19
+ # Build conversation history
20
+ conversation = ""
21
+ for user_msg, bot_msg in history:
22
+ conversation += f"{user_msg}{tokenizer.eos_token}{bot_msg}{tokenizer.eos_token}"
23
+
24
+ # Add current message
25
+ conversation += f"{message}{tokenizer.eos_token}"
26
+
27
+ # Tokenize
28
+ input_ids = tokenizer.encode(conversation, return_tensors="pt")
29
+
30
+ # Limit input length to prevent memory issues
31
+ if input_ids.shape[1] > 800:
32
+ input_ids = input_ids[:, -800:]
33
+
34
+ # Generate response
35
+ with torch.no_grad():
36
+ output = model.generate(
37
+ input_ids,
38
+ max_new_tokens=100,
39
+ do_sample=True,
40
+ top_p=0.9,
41
+ temperature=0.8,
42
+ pad_token_id=tokenizer.eos_token_id,
43
+ eos_token_id=tokenizer.eos_token_id,
44
+ no_repeat_ngram_size=2
45
+ )
46
+
47
+ # Decode response
48
+ response = tokenizer.decode(output[0][input_ids.shape[1]:], skip_special_tokens=True)
49
+ return response.strip() or "I'm not sure how to respond to that."
50
+
51
+ except Exception as e:
52
+ print(f"Error: {e}")
53
+ return "Sorry, I encountered an error. Please try again."
54
 
55
+ # Create Gradio interface
56
+ with gr.Blocks(title="DialoGPT Chatbot") as demo:
57
+ gr.Markdown("# 🤖 DialoGPT-medium Chatbot")
58
+ gr.Markdown("Chat with Microsoft's DialoGPT-medium model!")
59
+
60
+ chatbot = gr.Chatbot()
61
+ msg = gr.Textbox(placeholder="Type your message here...", container=False, scale=7)
62
+ clear = gr.Button("Clear Chat")
63
+
64
+ def user(user_message, history):
65
+ return "", history + [[user_message, None]]
66
+
67
+ def bot(history):
68
+ if history and history[-1][1] is None:
69
+ user_message = history[-1][0]
70
+ bot_response = respond(user_message, history[:-1])
71
+ history[-1][1] = bot_response
72
+ return history
73
+
74
+ msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
75
+ bot, chatbot, chatbot
76
  )
77
+ clear.click(lambda: None, None, chatbot, queue=False)
 
 
78
 
79
+ # Add example prompts
80
+ gr.Examples(
81
+ examples=[
82
+ "Hello, how are you?",
83
+ "What's your favorite movie?",
84
+ "Tell me a joke",
85
+ "What do you think about AI?"
86
+ ],
87
+ inputs=msg
88
+ )
 
89
 
 
90
  if __name__ == "__main__":
91
+ demo.queue()
92
+ demo.launch()
requirements.txt CHANGED
@@ -1,3 +1,3 @@
1
- torch
2
- transformers
3
- gradio
 
1
+ torch>=2.0.0,<2.2.0
2
+ transformers>=4.30.0,<4.40.0
3
+ gradio>=3.50.0,<4.0.0