JavierGon12 commited on
Commit
96c3959
1 Parent(s): f7eb7bd

Text Genereration script for chatbot

Browse files
Files changed (1) hide show
  1. pages/Text Generation.py +33 -17
pages/Text Generation.py CHANGED
@@ -1,25 +1,41 @@
1
  import streamlit as st
2
- from streamlit_mic_recorder import mic_recorder,speech_to_text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
- state=st.session_state
5
 
6
- if 'text_received' not in state:
7
- state.text_received=[]
8
 
9
- c1,c2=st.columns(2)
10
- with c1:
11
- st.write("Convert speech to text:")
12
- with c2:
13
- text=speech_to_text(language='en',use_container_width=True,just_once=True,key='STT')
14
 
15
- if text:
16
- state.text_received.append(text)
 
17
 
18
- for text in state.text_received:
19
- st.text(text)
20
 
21
- st.write("Record your voice, and play the recorded audio:")
22
- audio=mic_recorder(start_prompt="⏺️",stop_prompt="⏹️",key='recorder')
23
 
24
- if audio:
25
- st.audio(audio['bytes'])
 
 
 
 
1
  import streamlit as st
2
+ from PIL import Image
3
+ import streamlit as st
4
+ from transformers import pipeline
5
+ import pandas as pd
6
+ import plotly.express as px
7
+ import matplotlib.pyplot as plt
8
+ from pathlib import Path
9
+ import base64
10
+ from st_pages import Page, add_page_title, show_pages
11
+ from streamlit_extras.badges import badge
12
+ import transformers
13
+
14
+
15
+
16
+
17
+ model_name = 'Intel/neural-chat-7b-v3-1'
18
+ model = transformers.AutoModelForCausalLM.from_pretrained(model_name)
19
+ tokenizer = transformers.AutoTokenizer.from_pretrained(model_name)
20
 
21
+ def generate_response(system_input, user_input):
22
 
23
+ # Format the input using the provided template
24
+ prompt = f"### System:\n{system_input}\n### User:\n{user_input}\n### Assistant:\n"
25
 
26
+ # Tokenize and encode the prompt
27
+ inputs = tokenizer.encode(prompt, return_tensors="pt", add_special_tokens=False)
 
 
 
28
 
29
+ # Generate a response
30
+ outputs = model.generate(inputs, max_length=1000, num_return_sequences=1)
31
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
32
 
33
+ # Extract only the assistant's response
34
+ return response.split("### Assistant:\n")[-1]
35
 
 
 
36
 
37
+ # Example usage
38
+ system_input = "You are a employee in the customer succes department of a company called Retraced that works in sustainability and traceability"
39
+ prompt = st.text_input(str("Insert here you prompt?"))
40
+ response = generate_response(system_input, prompt)
41
+ st.write(response)