spedrox-sac commited on
Commit
14ecb63
·
verified ·
1 Parent(s): ea6be21

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -21
app.py CHANGED
@@ -1,31 +1,27 @@
1
  import streamlit as st
2
  from transformers import pipeline
3
- from langchain_core.output_parsers import StrOutputParser
4
 
5
- # Initialize the text generation pipeline
6
- model_name = "Qwen/Qwen2.5-0.5B-Instruct"
7
- pipe = pipeline("text-generation", model=model_name, device=-1)
8
- parser = StrOutputParser()
 
 
 
9
 
10
  # Streamlit app
11
- st.title("Optimized Batch Text Generation with Qwen Model")
12
 
13
  # Text input from the user
14
- user_input = st.text_area("Enter your messages (one per line):", "Who are you?\nWhat is your purpose?")
15
 
16
  # Generate text when the button is clicked
17
  if st.button("Generate"):
18
- # Split input into multiple messages
19
- user_messages = user_input.splitlines()
20
- messages = [message.strip() for message in user_messages if message.strip()]
21
-
22
- # Process messages in a batch
23
- outputs = pipe(messages, max_new_tokens=50) # Adjust max_new_tokens as needed
24
-
25
- # Display the generated text for each input message
26
- st.write("Generated Responses:")
27
- for i, output in enumerate(outputs):
28
- generated_text = output['generated_text']
29
- result = parser.invoke(generated_text)
30
- st.write(f"Input {i+1}: {messages[i]}")
31
- st.write(f"Response {i+1}: {result}\n")
 
1
  import streamlit as st
2
  from transformers import pipeline
 
3
 
4
+ # Initialize the text generation pipeline with optimizations
5
+ pipe = pipeline(
6
+ "text-generation",
7
+ model="Qwen/Qwen2.5-0.5B-Instruct",
8
+ device=-1, # Ensure it runs on CPU
9
+ use_fast=True, # Use fast tokenizer
10
+ )
11
 
12
  # Streamlit app
13
+ st.title("Optimized Text Generation with Qwen Model")
14
 
15
  # Text input from the user
16
+ user_input = st.text_input("Enter your message:", "Who are you?")
17
 
18
  # Generate text when the button is clicked
19
  if st.button("Generate"):
20
+ messages = [{"role": "user", "content": user_input}]
21
+ # Reduce max_new_tokens for faster generation
22
+ output = pipe(messages, max_new_tokens=30) # Adjust as needed for speed
23
+ generated_text = output[0]['generated_text']
24
+
25
+ # Display the generated text
26
+ st.write("Generated Response:")
27
+ st.write(generated_text)