HassanDataSci commited on
Commit
b6e9288
·
verified ·
1 Parent(s): 1d9d890

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -80
app.py CHANGED
@@ -1,97 +1,51 @@
1
  import streamlit as st
2
- from transformers import pipeline
3
  from gtts import gTTS
4
- import requests
5
  import os
6
 
7
- # Initialize the text generation pipeline with GPT-2
8
- generator = pipeline('text-generation', model='gpt2')
9
 
10
- # Initialize Whisper model for transcription
11
- transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-base")
12
-
13
- # Function to generate a Trump-like response
14
  def generate_response(prompt):
15
- # This is your original `generate_response` function
16
- response = generator(
17
- prompt,
18
- max_length=100, # Adjust length as needed
19
- num_return_sequences=1, # Generate only one response
20
- temperature=0.7, # Adjust for more randomness (higher) or more coherence (lower)
21
- )
22
- return response[0]['generated_text']
23
-
24
- # Uberduck API keys - Replace with your actual Uberduck API credentials
25
- UBERDUCK_API_KEY = 'pub_wxfzmwowyumqxfnwcc' # Replace with your actual Uberduck API Key
26
- UBERDUCK_API_SECRET = 'pk_8e4b3ad9-d26a-49ca-ac83-4ddfe4dace85' # Replace with your actual Uberduck API Secret
27
-
28
- # Function to generate audio using Uberduck API
29
- def generate_audio_uberduck(text):
30
- url = "https://api.uberduck.ai/speak"
31
- headers = {
32
- "Accept": "application/json",
33
- "uberduck-api-key": UBERDUCK_API_KEY,
34
- "uberduck-api-secret": UBERDUCK_API_SECRET
35
- }
36
- json_data = {
37
- "speech": text,
38
- "voice": "donald-trump"
39
- }
40
 
41
- # Send a request to Uberduck's API to generate audio
42
- response = requests.post(url, headers=headers, json=json_data)
43
- if response.status_code == 200:
44
- audio_url = response.json().get("path")
45
- # Download the audio file from the URL
46
- audio_data = requests.get(audio_url).content
47
- with open("output.mp3", "wb") as file:
48
- file.write(audio_data)
49
- return "output.mp3"
50
- else:
51
- st.write("Error generating audio:", response.text)
52
- return None
53
 
54
- # Alternative function to generate audio using gTTS
55
- def generate_audio_gtts(text):
56
  tts = gTTS(text=text, lang="en")
57
  audio_path = "output.mp3"
58
  tts.save(audio_path)
59
  return audio_path
60
 
61
- # Function to transcribe audio to text using Whisper
62
- def transcribe_audio(file_path):
63
- # Transcribe audio using Whisper model
64
- transcription = transcriber(file_path)
65
- return transcription['text']
66
-
67
  # Streamlit app UI
68
- st.title("Trump-like Voice Assistant")
69
- st.write("Record or upload an audio clip and receive a 'Trump-style' response.")
70
 
71
- # Upload or record audio
72
- uploaded_audio = st.file_uploader("Upload an audio file (wav format preferred)", type=["wav", "mp3"])
73
 
74
- if uploaded_audio is not None:
75
- # Save the uploaded audio file temporarily
76
- audio_path = "input_audio.wav"
77
- with open(audio_path, "wb") as f:
78
- f.write(uploaded_audio.getbuffer())
 
 
 
 
79
 
80
- # Transcribe audio to text
81
- user_input = transcribe_audio(audio_path)
82
- if user_input:
83
- st.write(f"Transcribed text: {user_input}")
84
-
85
- # Generate Trump-like response
86
- trump_prompt = f"As a bold and confident leader, respond to: {user_input}"
87
- trump_response = generate_response(trump_prompt)
88
- st.write("Trump-like Assistant:", trump_response)
89
-
90
- # Generate audio response (choose either Uberduck or gTTS)
91
- audio_output_path = generate_audio_uberduck(trump_response) # Uberduck
92
- # audio_output_path = generate_audio_gtts(trump_response) # gTTS as fallback
93
-
94
- if audio_output_path:
95
- audio_file = open(audio_output_path, "rb")
96
- audio_bytes = audio_file.read()
97
- st.audio(audio_bytes, format="audio/mp3")
 
1
  import streamlit as st
2
+ import openai
3
  from gtts import gTTS
 
4
  import os
5
 
6
+ # Set your OpenAI API key
7
+ openai.api_key = 'your_openai_api_key' # Replace with your actual OpenAI API key
8
 
9
+ # Function to generate Trump-like response using OpenAI's GPT-3.5 or GPT-4
 
 
 
10
  def generate_response(prompt):
11
+ # Craft the prompt to instruct the model to respond in a Trump-like manner
12
+ trump_prompt = f"As if you were Donald Trump, respond to the following: {prompt}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
+ # Call OpenAI API to generate response
15
+ response = openai.ChatCompletion.create(
16
+ model="gpt-3.5-turbo", # Or "gpt-4" if available in your account
17
+ messages=[
18
+ {"role": "system", "content": "You are to respond as Donald Trump would."},
19
+ {"role": "user", "content": trump_prompt}
20
+ ],
21
+ max_tokens=100,
22
+ temperature=0.8
23
+ )
24
+ return response.choices[0].message['content']
 
25
 
26
+ # Function to convert text to audio using gTTS
27
+ def generate_audio(text):
28
  tts = gTTS(text=text, lang="en")
29
  audio_path = "output.mp3"
30
  tts.save(audio_path)
31
  return audio_path
32
 
 
 
 
 
 
 
33
  # Streamlit app UI
34
+ st.title("Trump-like Text-to-Speech Assistant")
35
+ st.write("Type a question or statement, and receive a 'Trump-style' audio response!")
36
 
37
+ # Text input from user
38
+ user_input = st.text_input("Your message:", "Enter a message here")
39
 
40
+ if user_input:
41
+ # Generate Trump-like response
42
+ trump_response = generate_response(user_input)
43
+ st.write("Trump-like Assistant:", trump_response)
44
+
45
+ # Convert response to audio
46
+ audio_output_path = generate_audio(trump_response)
47
+ audio_file = open(audio_output_path, "rb")
48
+ audio_bytes = audio_file.read()
49
 
50
+ # Display audio output
51
+ st.audio(audio_bytes, format="audio/mp3")