Update app.py
Browse files
app.py
CHANGED
@@ -1,57 +1,88 @@
|
|
1 |
-
|
2 |
import streamlit as st
|
3 |
-
|
4 |
-
import torch
|
5 |
-
import torchvision.transforms as transforms
|
6 |
-
from animegan2.model import Generator
|
7 |
import os
|
8 |
-
import
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import tempfile
|
|
|
|
|
|
|
3 |
import os
|
4 |
+
import uuid
|
5 |
+
import torch
|
6 |
+
import json
|
7 |
+
from datetime import datetime
|
8 |
+
from transformers import pipeline
|
9 |
+
import coqui_tts
|
10 |
+
from TTS.api import TTS
|
11 |
+
import whisper
|
12 |
+
import requests
|
13 |
+
|
14 |
+
# Initialize Whisper model (tiny)
|
15 |
+
whisper_model = whisper.load_model("tiny")
|
16 |
+
|
17 |
+
# Load TTS model (Coqui TTS)
|
18 |
+
tts = TTS(model_name="tts_models/en/vctk/vits")
|
19 |
+
|
20 |
+
# Conversation history
|
21 |
+
if "history" not in st.session_state:
|
22 |
+
st.session_state.history = []
|
23 |
+
|
24 |
+
st.set_page_config(page_title="Voice Chat App", layout="centered")
|
25 |
+
st.title("π£οΈ Voice-Based Conversational App")
|
26 |
+
|
27 |
+
st.sidebar.header("ποΈ Settings")
|
28 |
+
language = st.sidebar.selectbox("Select Language", ["en", "es", "fr", "de", "it"])
|
29 |
+
emotion = st.sidebar.selectbox("Select Emotion", ["neutral", "happy", "sad", "angry"])
|
30 |
+
voice_avatar = st.sidebar.selectbox("Select Voice Avatar", ["female", "male"])
|
31 |
+
|
32 |
+
st.markdown("Speak or upload a voice file to start chatting with the AI")
|
33 |
+
|
34 |
+
# Voice input
|
35 |
+
voice_input = st.file_uploader("Upload a voice file", type=["wav", "mp3", "m4a"])
|
36 |
+
|
37 |
+
if voice_input:
|
38 |
+
# Save temporary file
|
39 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
|
40 |
+
tmp_file.write(voice_input.read())
|
41 |
+
tmp_path = tmp_file.name
|
42 |
+
|
43 |
+
# Transcribe using Whisper
|
44 |
+
st.info("Transcribing...")
|
45 |
+
result = whisper_model.transcribe(tmp_path, language=language)
|
46 |
+
user_text = result["text"]
|
47 |
+
st.success(f"You said: {user_text}")
|
48 |
+
|
49 |
+
# Chat with Groq API (LLaMA3 or Mixtral)
|
50 |
+
st.info("Generating response...")
|
51 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
52 |
+
headers = {
|
53 |
+
"Authorization": f"Bearer {GROQ_API_KEY}",
|
54 |
+
"Content-Type": "application/json"
|
55 |
+
}
|
56 |
+
data = {
|
57 |
+
"model": "mixtral-8x7b-32768",
|
58 |
+
"messages": [{"role": "user", "content": user_text}],
|
59 |
+
"temperature": 0.7
|
60 |
+
}
|
61 |
+
response = requests.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, data=json.dumps(data))
|
62 |
+
reply_text = response.json()["choices"][0]["message"]["content"]
|
63 |
+
st.success(f"AI: {reply_text}")
|
64 |
+
|
65 |
+
# Save history
|
66 |
+
st.session_state.history.append({"user": user_text, "bot": reply_text})
|
67 |
+
|
68 |
+
# Convert to speech
|
69 |
+
st.info("Generating voice reply...")
|
70 |
+
output_path = f"output_{uuid.uuid4().hex}.wav"
|
71 |
+
tts.tts_to_file(text=reply_text, file_path=output_path, speaker=0 if voice_avatar == "female" else 1)
|
72 |
+
|
73 |
+
# Play response
|
74 |
+
audio_file = open(output_path, "rb")
|
75 |
+
audio_bytes = audio_file.read()
|
76 |
+
st.audio(audio_bytes, format="audio/wav")
|
77 |
+
|
78 |
+
# Clean up
|
79 |
+
os.remove(tmp_path)
|
80 |
+
os.remove(output_path)
|
81 |
+
|
82 |
+
# Show conversation history
|
83 |
+
if st.session_state.history:
|
84 |
+
st.subheader("π Conversation History")
|
85 |
+
for i, item in enumerate(st.session_state.history):
|
86 |
+
st.markdown(f"**You:** {item['user']}")
|
87 |
+
st.markdown(f"**AI:** {item['bot']}")
|
88 |
+
st.markdown("---")
|