Spaces:
Sleeping
Sleeping
File size: 2,154 Bytes
dbd0a3c 06ec86b dbd0a3c 06ec86b f20b251 dbd0a3c d2e841b 06ec86b 619f3f9 d2e841b 06ec86b dbd0a3c 06ec86b d2e841b dbd0a3c 06ec86b d2e841b 06ec86b dbd0a3c b6e9288 dbd0a3c 619f3f9 06ec86b 40c942f 619f3f9 f20b251 619f3f9 b6e9288 40c942f f20b251 b6e9288 dbd0a3c f20b251 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
import streamlit as st
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
from gtts import gTTS
import torch
import os
# Load DialoGPT model for conversational style
model_name = "microsoft/DialoGPT-medium"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
# Set up a text generation pipeline with the conversational model
generator = pipeline("text-generation", model=model, tokenizer=tokenizer, device=0 if torch.cuda.is_available() else -1)
# Function to generate Trump-like response
def generate_response(prompt):
# Craft the prompt to encourage a Trump-like response
trump_prompt = f"Imagine you are Donald Trump. Respond in your style to: {prompt}"
# Generate the response
response = generator(trump_prompt, max_length=50, num_return_sequences=1, temperature=0.6)
return response[0]['generated_text']
# Function to convert text to audio using gTTS
def generate_audio(text):
tts = gTTS(text=text, lang="en")
audio_path = "output.mp3"
tts.save(audio_path)
return audio_path
# Streamlit app UI
st.title("Trump-like Chat Assistant")
st.write("Type in a question or statement, and receive a 'Trump-style' response in both text and audio!")
# Text input from user
user_input = st.text_input("Your message:", "Enter a message here")
if user_input:
# Generate Trump-like response
trump_response = generate_response(user_input)
# Display text output of the response
st.subheader("Trump-like Assistant (Text Response):")
st.write(trump_response) # Show the generated text directly
# Convert response to audio
audio_output_path = generate_audio(trump_response)
# Ensure the file exists and can be played back
if os.path.exists(audio_output_path):
with open(audio_output_path, "rb") as audio_file:
audio_bytes = audio_file.read()
# Display audio output
st.subheader("Trump-like Assistant (Audio Response):")
st.audio(audio_bytes, format="audio/mp3")
else:
st.error("Failed to generate audio. Please try again.") |