meraj12 commited on
Commit
b67ca14
Β·
verified Β·
1 Parent(s): e45706a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -55
app.py CHANGED
@@ -1,57 +1,88 @@
1
-
2
  import streamlit as st
3
- from PIL import Image
4
- import torch
5
- import torchvision.transforms as transforms
6
- from animegan2.model import Generator
7
  import os
8
- import urllib.request
9
-
10
- st.set_page_config(page_title="Ghibli Style Image Converter 🎨")
11
-
12
- MODEL_PATH = "weights/paprika.pt"
13
- MODEL_URL = "https://huggingface.co/akhaliq/AnimeGANv2/resolve/main/paprika.pt"
14
-
15
- @st.cache_resource
16
- def load_model():
17
- if not os.path.exists(MODEL_PATH):
18
- os.makedirs("weights", exist_ok=True)
19
- with st.spinner("Downloading model weights..."):
20
- urllib.request.urlretrieve(MODEL_URL, MODEL_PATH)
21
- st.success("Model downloaded successfully!")
22
- model = Generator()
23
- model.load_state_dict(torch.load(MODEL_PATH, map_location="cpu"))
24
- model.eval()
25
- return model
26
-
27
- def preprocess(image):
28
- image = image.convert("RGB")
29
- transform = transforms.Compose([
30
- transforms.Resize((256, 256)),
31
- transforms.ToTensor()
32
- ])
33
- return transform(image).unsqueeze(0)
34
-
35
- def postprocess(tensor):
36
- tensor = tensor.squeeze().detach().cpu().clamp(0, 1)
37
- image = transforms.ToPILImage()(tensor)
38
- return image
39
-
40
- st.title("🎨 Convert to Ghibli Style!")
41
- st.markdown("Upload an image to transform it into a beautiful Ghibli-style artwork using AnimeGAN2!")
42
-
43
- uploaded_file = st.file_uploader("Upload your image", type=["jpg", "jpeg", "png"])
44
-
45
- if uploaded_file:
46
- input_image = Image.open(uploaded_file)
47
- st.image(input_image, caption="Original Image", use_column_width=True)
48
-
49
- with st.spinner("Converting... please wait!"):
50
- model = load_model()
51
- input_tensor = preprocess(input_image)
52
- with torch.no_grad():
53
- output_tensor = model(input_tensor)
54
- output_image = postprocess(output_tensor)
55
-
56
- st.image(output_image, caption="Ghibli Style Image", use_column_width=True)
57
- st.success("Done!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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("---")