Spaces:
Runtime error
Runtime error
JavierGon12
commited on
Commit
•
96c3959
1
Parent(s):
f7eb7bd
Text Genereration script for chatbot
Browse files- pages/Text Generation.py +33 -17
pages/Text Generation.py
CHANGED
@@ -1,25 +1,41 @@
|
|
1 |
import streamlit as st
|
2 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
-
|
5 |
|
6 |
-
|
7 |
-
|
8 |
|
9 |
-
|
10 |
-
|
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 |
-
|
16 |
-
|
|
|
17 |
|
18 |
-
|
19 |
-
|
20 |
|
21 |
-
st.write("Record your voice, and play the recorded audio:")
|
22 |
-
audio=mic_recorder(start_prompt="⏺️",stop_prompt="⏹️",key='recorder')
|
23 |
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
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)
|