igortech commited on
Commit
59ec9b8
·
verified ·
1 Parent(s): 9dcd236

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -23
app.py CHANGED
@@ -1,32 +1,34 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
- # Load a text generation model
5
- generator = pipeline(
6
  "text-generation",
7
- model="microsoft/DialoGPT-small",
8
- max_length=200,
9
- pad_token_id=50256
10
  )
11
 
12
- def chat_fn(message, history):
13
- # Flatten history into a single prompt
14
- prompt_text = ""
15
- for user, bot in history:
16
- prompt_text += f"User: {user}\nBot: {bot}\n"
17
- prompt_text += f"User: {message}\nBot:"
18
 
19
- # Generate reply
20
- response = generator(prompt_text, num_return_sequences=1)
21
- bot_reply = response[0]['generated_text'].split("Bot:")[-1].strip()
22
- return bot_reply
23
 
24
- demo = gr.ChatInterface(
25
- fn=chat_fn,
26
- title="🤖 My Hugging Face Chatbot",
27
- description="A chatbot running on Hugging Face Spaces with conversation memory.",
28
- examples=["Hello!", "Tell me a joke", "What's the capital of France?"]
29
- )
 
 
 
 
 
 
30
 
31
- if __name__ == "__main__":
32
- demo.launch()
 
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
+ # Load the model
5
+ chatbot_pipeline = pipeline(
6
  "text-generation",
7
+ model="microsoft/DialoGPT-small"
 
 
8
  )
9
 
10
+ # Define a function to handle the conversation
11
+ def respond(message, history=None):
12
+ if history is None:
13
+ history = []
 
 
14
 
15
+ # Merge previous messages into a single conversation string
16
+ conversation = ""
17
+ for user_msg, bot_msg in history:
18
+ conversation += f"User: {user_msg}\nBot: {bot_msg}\n"
19
 
20
+ conversation += f"User: {message}\nBot:"
21
+
22
+ # Generate response
23
+ output = chatbot_pipeline(
24
+ conversation,
25
+ max_length=200,
26
+ pad_token_id=50256
27
+ )
28
+
29
+ reply = output[0]['generated_text'].split("Bot:")[-1].strip()
30
+ history.append((message, reply))
31
+ return reply, history
32
 
33
+ # Create the Gradio interface
34
+ gr.ChatInterface(fn=respond).launch()