monesh2212 commited on
Commit
bc1efdb
·
verified ·
1 Parent(s): 6e9f53b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -41
app.py CHANGED
@@ -1,41 +1,51 @@
1
- # app.py
2
-
3
- import gradio as gr
4
- from transformers import pipeline, Conversation
5
-
6
- # 1) Create a conversational pipeline with Decryptu/distilgpt2
7
- chat_pipeline = pipeline("conversational", model="Decryptu/distilgpt2")
8
-
9
- def respond(user_message, chat_history):
10
- """
11
- Receives a user message and a running conversation history,
12
- then uses the pipeline to generate a response.
13
- """
14
- # Create a new Conversation object for the user's message
15
- conversation = Conversation(user_message)
16
-
17
- # Generate a response using the pipeline
18
- conversation = chat_pipeline(conversation)
19
- bot_response = conversation.generated_responses[-1]
20
-
21
- # Append user input and model response to chat history
22
- chat_history.append((user_message, bot_response))
23
-
24
- # Return empty input box and updated chat history
25
- return "", chat_history
26
-
27
- # 2) Build the Gradio UI
28
- with gr.Blocks() as demo:
29
- gr.Markdown("## DistilGPT2 Chatbot")
30
- chatbot = gr.Chatbot(label="Chat Window")
31
- user_input = gr.Textbox(placeholder="Ask a question or start a conversation...")
32
- clear_btn = gr.Button("Clear Chat")
33
-
34
- # Whenever the user submits text, call the respond function
35
- user_input.submit(fn=respond, inputs=[user_input, chatbot], outputs=[user_input, chatbot])
36
-
37
- # Clear the conversation history when the user clicks 'Clear Chat'
38
- clear_btn.click(lambda: [], None, chatbot, queue=False)
39
-
40
- # 3) Launch the Gradio interface
41
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import pipeline
3
+
4
+ # Create a text-generation pipeline (no Conversation class needed)
5
+ model_name = "Decryptu/distilgpt2"
6
+ generator = pipeline("text-generation", model=model_name)
7
+
8
+ # Store chat history in session_state
9
+ if "history" not in st.session_state:
10
+ st.session_state.history = []
11
+
12
+ st.title("Chat with DistilGPT2")
13
+ st.write("A simple chat-like interface using a text-generation pipeline.")
14
+
15
+ # User input
16
+ user_input = st.text_input("Type your message here:")
17
+
18
+ # On 'Send' button click
19
+ if st.button("Send"):
20
+ if user_input.strip():
21
+ # 1) Append user's message to the history
22
+ st.session_state.history.append(("User", user_input))
23
+
24
+ # 2) Build a prompt from the entire conversation
25
+ prompt = ""
26
+ for speaker, text in st.session_state.history:
27
+ prompt += f"{speaker}: {text}\n"
28
+ prompt += "Bot: "
29
+
30
+ # 3) Generate text from the model
31
+ output = generator(
32
+ prompt,
33
+ max_length=100,
34
+ do_sample=True,
35
+ temperature=0.7
36
+ )
37
+
38
+ # The model output includes the prompt plus new text
39
+ bot_text = output[0]["generated_text"].split("Bot:", 1)[-1].strip()
40
+
41
+ # 4) Append the bot's response to the history
42
+ st.session_state.history.append(("Bot", bot_text))
43
+
44
+ # Display the conversation
45
+ st.write("### Conversation")
46
+ for speaker, text in st.session_state.history:
47
+ st.write(f"**{speaker}:** {text}")
48
+
49
+ # Add a 'Clear Chat' button (optional)
50
+ if st.button("Clear Chat"):
51
+ st.session_state.history = []