Hhhh / coder.py
Kfjjdjdjdhdhd's picture
Upload 26 files
7b74407 verified
import dash
from dash import Dash, html, dcc, callback, Output, Input, State
from dash.exceptions import PreventUpdate
import base64, uuid, requests, io, re
app_style = None
external_stylesheets = ['style.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div([dcc.Location(id='url', refresh=False), html.Div(id='page-content')])
index_page = html.Div([html.H1("AI Powerhouse", className='index-title animate__animated animate__fadeInDown'), html.Div([dcc.Link('Text Generation', href='/text-generation', className='index-link animate__animated animate__fadeInUp'), dcc.Link('Audio Video', href='/audio-video', className='index-link animate__animated animate__fadeInUp'), dcc.Link('Image Generation', href='/image-generation', className='index-link animate__animated animate__fadeInUp'), dcc.Link('Image to 3D', href='/image-to-3d', className='index-link animate__animated animate__fadeInUp'), dcc.Link('Text to Video', href='/text-to-video', className='index-link animate__animated animate__fadeInUp'), dcc.Link('Music Generation', href='/music-generation', className='index-link animate__animated animate__fadeInUp'), dcc.Link('Sentiment Analysis', href='/sentiment-analysis', className='index-link animate__animated animate__fadeInUp'), dcc.Link('Translation', href='/translation', className='index-link animate__animated animate__fadeInUp'), dcc.Link('Code Generation', href='/code-generation', className='index-link animate__animated animate__fadeInUp'), dcc.Link('Summarization', href='/summarization', className='index-link animate__animated animate__fadeInUp'), dcc.Link('SadTalker', href='/sad-talker', className='index-link animate__animated animate__fadeInUp')], className='index-links-container')], className='index-page page-layout')
text_generation_layout = html.Div([html.Div(className='animated-text animate__animated animate__fadeIn animate__infinite infinite', children='AI POWERED Text Generation'), html.Div(className='chat-container page-layout', children=[html.Div(className='chat-header animate__animated animate__fadeInDown', children="Text Generation Interface"), html.Div(className='chat-form animate__animated animate__fadeInLeft', children=[dcc.Textarea(id='text-input', placeholder='Enter text prompt...', rows=4, className='chat-text-area'), html.Button('Generate', id='generate-button', n_clicks=0, className='chat-generate-button')]), html.Div(id='output', className='chat-output animate__animated animate__fadeInUp', children=[html.Div(className='response-header', children="Response:"), dcc.Markdown(id='generated-text', className='response-text')]), dcc.Link('Back to Home', href='/', className='chat-back-link')])], className='page-layout text-gen-layout')
audio_video_layout = html.Div([html.Div(className='animated-text animate__animated animate__fadeIn animate__infinite infinite', children='AI POWERED Audio & Video Tools'), html.Div(className='av-container page-layout', children=[html.Div(className='av-header animate__animated animate__fadeInDown', children="Audio & Video Processing"), html.Div(className='av-upload-section animate__animated animate__fadeInLeft', children=[html.Div(className='upload-box', children=[dcc.Upload(id='upload-audio', children=html.Div(['Drag and Drop or ', html.A('Select Audio')]), className='upload-area'), html.Div(id='audio-output-text', className='upload-output', children="STT Output")]), html.Div(className='upload-box', children=[dcc.Upload(id='upload-image', children=html.Div(['Drag and Drop or ', html.A('Select Image')]), className='upload-area'), html.Div(id='image-output', className='upload-output', children="Image Uploaded")])]), html.Div(id='video-output', className='av-video-output animate__animated animate__fadeInUp', children="Video Output Area"), dcc.Link('Back to Home', href='/', className='av-back-link')])], className='page-layout av-layout')
image_generation_layout = html.Div([html.Div(className='animated-text animate__animated animate__fadeIn animate__infinite infinite', children='AI POWERED Image Generation'), html.Div(className='imagegen-container page-layout', children=[html.Div(className='imagegen-header animate__animated animate__fadeInDown', children="Image Generation Interface"), html.Div(className='imagegen-form animate__animated animate__fadeInLeft', children=[dcc.Textarea(id='imagegen-text-input', placeholder='Enter prompt for image...', rows=4, className='imagegen-text-area'), html.Button('Generate Image', id='generate-image-button', n_clicks=0, className='imagegen-generate-button')]), html.Div(id='image-output-display', className='imagegen-output animate__animated animate__fadeInUp'), dcc.Link('Back to Home', href='/', className='imagegen-back-link')])], className='page-layout imagegen-layout')
image_to_3d_layout = html.Div([html.Div(className='animated-text animate__animated animate__fadeIn animate__infinite infinite', children='AI POWERED Image to 3D Conversion'), html.Div(className='imagetod-container page-layout', children=[html.Div(className='imagetod-header animate__animated animate__fadeInDown', children="Image to 3D Model Conversion"), html.Div(className='imagetod-upload animate__animated animate__fadeInLeft', children=[dcc.Upload(id='upload-image-3d', children=html.Div(['Drag and Drop or ', html.A('Select Image')]), className='imagetod-upload-area')]), html.Div(id='3d-model-output', className='imagetod-output animate__animated animate__fadeInUp'), dcc.Link('Back to Home', href='/', className='imagetod-back-link')])], className='page-layout imagetod-layout')
text_to_video_layout = html.Div([html.Div(className='animated-text animate__animated animate__fadeIn animate__infinite infinite', children='AI POWERED Text to Video Generation'), html.Div(className='textvideo-container page-layout', children=[html.Div(className='textvideo-header animate__animated animate__fadeInDown', children="Text to Video Generation Interface"), html.Div(className='textvideo-form animate__animated animate__fadeInLeft', children=[dcc.Textarea(id='text-video-input', placeholder='Enter prompt for video...', rows=4, className='textvideo-text-area'), html.Button('Generate Video', id='generate-video-button', n_clicks=0, className='textvideo-generate-button')]), html.Div(id='video-gen-output', className='textvideo-output animate__animated animate__fadeInUp'), dcc.Link('Back to Home', href='/', className='textvideo-back-link')])], className='page-layout textvideo-layout')
music_generation_layout = html.Div([html.Div(className='animated-text animate__animated animate__fadeIn animate__infinite infinite', children='AI POWERED Music Generation'), html.Div(className='musicgen-container page-layout', children=[html.Div(className='musicgen-header animate__animated animate__fadeInDown', children="Music Generation Interface"), html.Div(className='musicgen-form animate__animated animate__fadeInLeft', children=[dcc.Textarea(id='musicgen-text-input', placeholder='Enter prompt for music...', rows=4, className='musicgen-text-area'), html.Button('Generate Music', id='generate-music-button', n_clicks=0, className='musicgen-generate-button')]), html.Div(id='music-output-display', className='musicgen-output animate__animated animate__fadeInUp'), dcc.Link('Back to Home', href='/', className='musicgen-back-link')])], className='page-layout musicgen-layout')
sentiment_analysis_layout = html.Div([html.Div(className='animated-text animate__animated animate__fadeIn animate__infinite infinite', children='AI POWERED Sentiment Analysis'), html.Div(className='sentiment-container page-layout', children=[html.Div(className='sentiment-header animate__animated animate__fadeInDown', children="Sentiment Analysis Tool"), html.Div(className='sentiment-form animate__animated animate__fadeInLeft', children=[dcc.Textarea(id='sentiment-text-input', placeholder='Enter text for analysis...', rows=4, className='sentiment-text-area'), html.Button('Analyze Sentiment', id='analyze-sentiment-button', n_clicks=0, className='sentiment-analyze-button')]), html.Div(id='sentiment-output-display', className='sentiment-output animate__animated animate__fadeInUp'), dcc.Link('Back to Home', href='/', className='sentiment-back-link')])], className='page-layout sentiment-layout')
translation_layout = html.Div([html.Div(className='animated-text animate__animated animate__fadeIn animate__infinite infinite', children='AI POWERED Translation Services'), html.Div(className='translation-container page-layout', children=[html.Div(className='translation-header animate__animated animate__fadeInDown', children="Translation Interface"), html.Div(className='translation-form animate__animated animate__fadeInLeft', children=[dcc.Textarea(id='translate-text-input', placeholder='Enter text to translate...', rows=4, className='translation-text-area'), dcc.Dropdown(id='target-language-dropdown', options=[{'label': 'Spanish', 'value': 'es'},{'label': 'English', 'value': 'en'},{'label': 'French', 'value': 'fr'},{'label': 'German', 'value': 'de'}], value='es', className='translation-dropdown'), html.Button('Translate', id='translate-button', n_clicks=0, className='translation-translate-button')]), html.Div(id='translation-output-display', className='translation-output animate__animated animate__fadeInUp'), dcc.Link('Back to Home', href='/', className='translation-back-link')])], className='page-layout translation-layout')
code_generation_layout = html.Div([html.Div(className='animated-text animate__animated animate__fadeIn animate__infinite infinite', children='AI POWERED Code Generation'), html.Div(className='codegen-container page-layout', children=[html.Div(className='codegen-header animate__animated animate__fadeInDown', children="Code Generation Interface"), html.Div(className='codegen-form animate__animated animate__fadeInLeft', children=[dcc.Textarea(id='codegen-text-input', placeholder='Enter prompt for code...', rows=4, className='codegen-text-area'), html.Button('Generate Code', id='generate-code-button', n_clicks=0, className='codegen-generate-button')]), html.Div(id='codegen-output-display', className='codegen-output animate__animated animate__fadeInUp'), dcc.Link('Back to Home', href='/', className='codegen-back-link')])], className='page-layout codegen-layout')
summarization_layout = html.Div([html.Div(className='animated-text animate__animated animate__fadeIn animate__infinite infinite', children='AI POWERED Text Summarization'), html.Div(className='summarization-container page-layout', children=[html.Div(className='summarization-header animate__animated animate__fadeInDown', children="Text Summarization Tool"), html.Div(className='summarization-form animate__animated animate__fadeInLeft', children=[dcc.Textarea(id='summarize-text-input', placeholder='Enter text to summarize...', rows=4, className='summarization-text-area'), html.Button('Summarize', id='summarize-button', n_clicks=0, className='summarization-summarize-button')]), html.Div(id='summarization-output-display', className='summarization-output animate__animated animate__fadeInUp'), dcc.Link('Back to Home', href='/', className='summarization-back-link')])], className='page-layout summarization-layout')
sadtalker_layout = html.Div([html.Div(className='animated-text animate__animated animate__fadeIn animate__infinite infinite', children='AI POWERED SadTalker'), html.Div(className='sadtalker-container page-layout', children=[html.Div(className='sadtalker-header animate__animated animate__fadeInDown', children="SadTalker Interface"), html.Div(className='sadtalker-upload animate__animated animate__fadeInLeft', children=[dcc.Upload(id='upload-sadtalker-image', children=html.Div(['Drag and Drop Image', html.Br(), html.I(className="fas fa-image upload-icon")]), className='sadtalker-upload-area', multiple=False), dcc.Upload(id='upload-sadtalker-audio', children=html.Div(['Drag and Drop Audio', html.Br(), html.I(className="fas fa-file-audio upload-icon")]), className='sadtalker-upload-area', multiple=False)]), html.Div(id='sadtalker-video-output', className='sadtalker-output animate__animated animate__fadeInUp'), dcc.Link('Back to Home', href='/', className='sadtalker-back-link')])], className='page-layout sadtalker-layout')
@app.callback(Output('page-content', 'children'), [Input('url', 'pathname')])
def display_page(pathname):
if pathname == '/text-generation': return text_generation_layout
elif pathname == '/audio-video': return audio_video_layout
elif pathname == '/image-generation': return image_generation_layout
elif pathname == '/image-to-3d': return image_to_3d_layout
elif pathname == '/text-to-video': return text_to_video_layout
elif pathname == '/music-generation': return music_generation_layout
elif pathname == '/sentiment-analysis': return sentiment_analysis_layout
elif pathname == '/translation': return translation_layout
elif pathname == '/code-generation': return code_generation_layout
elif pathname == '/summarization': return summarization_layout
elif pathname == '/sad-talker': return sadtalker_layout
else: return index_page
@app.callback(Output('generated-text', 'children'), Output('output', 'className'), Input('generate-button', 'n_clicks'), State('text-input', 'value'), prevent_initial_call=True)
def generate_reasoning_dash(n_clicks, text_input):
if not text_input: return "Please enter text.", 'chat-output animate__animated animate__fadeInUp error-output'
api_url = "/api/v1/generate"; payload = {"text": text_input}
try: response = requests.post("http://127.0.0.1:7860" + api_url, json=payload); response.raise_for_status(); data = response.json(); generated_text = data.get("response", "Error in backend response."); return generated_text, 'chat-output animate__animated animate__fadeInUp'
except requests.exceptions.RequestException as e: return f"Error communicating with backend: {e}", 'chat-output animate__animated animate__fadeInUp error-output'
@app.callback(Output('audio-output-text', 'children'), Output('video-output', 'children'), Output('audio-output-text', 'className'), Output('video-output', 'className'), Input('upload-audio', 'contents'), State('upload-audio', 'filename'), Input('upload-image', 'contents'), State('upload-image', 'filename'), prevent_initial_call=True)
def process_audio_video_dash(audio_contents, audio_filename, image_contents, image_filename):
stt_output_text = ""; video_display = ""; stt_class = 'upload-output'; video_class = 'av-video-output animate__animated animate__fadeInUp'
if audio_contents:
try: content_type, content_string = audio_contents.split(','); decoded_audio = base64.b64decode(content_string); audio_io = io.BytesIO(decoded_audio); files = {'audio': (audio_filename, audio_io, content_type)}; response = requests.post("http://127.0.0.1:7860/api/v1/stt", files=files); response.raise_for_status(); data = response.json(); stt_output_text = f"STT Output: {data.get('text', 'Transcription failed')}"; stt_class = 'upload-output success'
except requests.exceptions.RequestException as e: stt_output_text = f"STT Error: {e}"; stt_class = 'upload-output error'
if image_contents:
try: content_type, content_string = image_contents.split(','); decoded_image = base64.b64decode(content_string); image_io = io.BytesIO(decoded_image); files = {'image': (image_filename, image_io, content_type)}; response = requests.post("http://127.0.0.1:7860/api/v1/image_to_3d", files=files); response.raise_for_status(); video_display = "3D Model Feature Extracted (Check Backend Logs for Output)."; video_class = 'av-video-output animate__animated animate__fadeInUp success'
except requests.exceptions.RequestException as e: video_display = f"3D Error: {e}"; video_class = 'av-video-output animate__animated animate__fadeInUp error'
video_output_component = html.Div(video_display) if video_display else ""; return stt_output_text, video_output_component, stt_class, video_class
@app.callback(Output('image-output-display', 'children'), Output('image-output-display', 'className'), Input('generate-image-button', 'n_clicks'), State('imagegen-text-input', 'value'), prevent_initial_call=True)
def generate_image_dash(n_clicks, prompt):
if not prompt: return "Please enter a prompt for image generation.", 'imagegen-output animate__animated animate__fadeInUp error-output'
api_url = "/api/v1/imagegen"; payload = {"prompt": prompt}
try: response = requests.post("http://127.0.0.1:7860" + api_url, json=payload); response.raise_for_status(); image_base64 = base64.b64encode(response.content).decode('utf-8'); return html.Img(src=f'data:image/png;base64,{image_base64}', className='generated-image'), 'imagegen-output animate__animated animate__fadeInUp success-output'
except requests.exceptions.RequestException as e: return f"Image Generation Error: {e}", 'imagegen-output animate__animated animate__fadeInUp error-output'
@app.callback(Output('3d-model-output', 'children'), Output('3d-model-output', 'className'), Input('upload-image-3d', 'contents'), State('upload-image-3d', 'filename'), prevent_initial_call=True)
def process_image_to_3d_dash(contents, filename):
if contents is None: raise PreventUpdate
try:
content_type, content_string = contents.split(',')
decoded_image = base64.b64decode(content_string); image_io = io.BytesIO(decoded_image); files = {'image': (filename, image_io, content_type)}
response = requests.post("http://127.0.0.1:7860/api/v1/image_to_3d", files=files); response.raise_for_status()
content_disposition = response.headers.get('Content-Disposition'); download_filename = 'model_3d.obj'
if content_disposition: filenames = re.findall('filename="([^"]+)"', content_disposition);
if filenames: download_filename = filenames[0]
model_base64 = base64.b64encode(response.content).decode('utf-8'); href = f'data:model/obj;base64,{model_base64}'
download_link = html.A('Download 3D Model', href=href, download=download_filename, className='download-link'),; return download_link, 'imagetod-output animate__animated animate__fadeInUp success-output'
except requests.exceptions.RequestException as e: return f"3D Conversion Error: {e}", 'imagetod-output animate__animated animate__fadeInUp error-output'
@app.callback(Output('video-gen-output', 'children'), Output('video-gen-output', 'className'), Input('generate-video-button', 'n_clicks'), State('text-video-input', 'value'), prevent_initial_call=True)
def generate_video_dash(n_clicks, prompt):
if not prompt: return "Please enter a prompt for video generation.", 'textvideo-output animate__animated animate__fadeInUp error-output'
api_url = "/api/v1/text_to_video"; payload = {"prompt": prompt}
try: response = requests.post("http://127.0.0.1:7860" + api_url, json=payload); response.raise_for_status(); video_base64 = base64.b64encode(response.content).decode('utf-8'); return html.Video(src=f'data:video/mp4;base64,{video_base64}', controls=True, className='generated-video'), 'textvideo-output animate__animated animate__fadeInUp success-output'
except requests.exceptions.RequestException as e: return f"Video Generation Error: {e}", 'textvideo-output animate__animated animate__fadeInUp error-output'
@app.callback(Output('music-output-display', 'children'), Output('music-output-display', 'className'), Input('generate-music-button', 'n_clicks'), State('musicgen-text-input', 'value'), prevent_initial_call=True)
def generate_music_dash(n_clicks, prompt):
if not prompt: return "Please enter a prompt for music generation.", 'musicgen-output animate__animated animate__fadeInUp error-output'
api_url = "/api/v1/musicgen"; payload = {"prompt": prompt}
try: response = requests.post("http://127.0.0.1:7860" + api_url, json=payload); response.raise_for_status(); audio_base64 = base64.b64encode(response.content).decode('utf-8'); return html.Audio(src=f'data:audio/wav;base64,{audio_base64}', controls=True, className='generated-audio'), 'musicgen-output animate__animated animate__fadeInUp success-output'
except requests.exceptions.RequestException as e: return f"Music Generation Error: {e}", 'musicgen-output animate__animated animate__fadeInUp error-output'
@app.callback(Output('sentiment-output-display', 'children'), Output('sentiment-output-display', 'className'), Input('analyze-sentiment-button', 'n_clicks'), State('sentiment-text-input', 'value'), prevent_initial_call=True)
def analyze_sentiment_dash(n_clicks, text):
if not text: return "Please enter text for sentiment analysis.", 'sentiment-output animate__animated animate__fadeInUp error-output'
api_url = "/api/v1/sentiment"; payload = {"text": text}
try: response = requests.post("http://127.0.0.1:7860" + api_url, json=payload); response.raise_for_status(); data = response.json(); sentiment_label = data.get('sentiment', 'Analysis Failed'); return f"Sentiment: {sentiment_label}", 'sentiment-output animate__animated animate__fadeInUp success-output'
except requests.exceptions.RequestException as e: return f"Sentiment Analysis Error: {e}", 'sentiment-output animate__animated animate__fadeInUp error-output'
@app.callback(Output('translation-output-display', 'children'), Output('translation-output-display', 'className'), Input('translate-button', 'n_clicks'), State('translate-text-input', 'value'), State('target-language-dropdown', 'value'), prevent_initial_call=True)
def translate_text_dash(n_clicks, text, target_lang):
if not text: return "Please enter text for translation.", 'translation-output animate__animated animate__fadeInUp error-output'
api_url = "/api/v1/translation"; payload = {"text": text, "target_lang": target_lang}
try: response = requests.post("http://127.0.0.1:7860" + api_url, json=payload); response.raise_for_status(); data = response.json(); translation = data.get('translated_text', 'Translation Failed'); return f"Translation ({target_lang.upper()}): {translation}", 'translation-output animate__animated animate__fadeInUp success-output'
except requests.exceptions.RequestException as e: return f"Translation Error: {e}", 'translation-output animate__animated animate__fadeInUp error-output'
@app.callback(Output('codegen-output-display', 'children'), Output('codegen-output-display', 'className'), Input('generate-code-button', 'n_clicks'), State('codegen-text-input', 'value'), prevent_initial_call=True)
def generate_code_dash(n_clicks, prompt):
if not prompt: return "Please enter a prompt for code generation.", 'codegen-output animate__animated animate__fadeInUp error-output'
api_url = "/api/v1/codegen"; payload = {"prompt": prompt}
try: response = requests.post("http://127.0.0.1:7860" + api_url, json=payload); response.raise_for_status()
content_disposition = response.headers.get('Content-Disposition'); download_filename = 'code.py'
if content_disposition: filenames = re.findall('filename="([^"]+)"', content_disposition); if filenames: download_filename = filenames[0]
code_base64 = base64.b64encode(response.content).decode('utf-8'); download_link = html.A('Download Code', href=f'data:text/x-python;base64,{code_base64}', download=download_filename, className='download-link'); return download_link, 'codegen-output animate__animated animate__fadeInUp success-output'
except requests.exceptions.RequestException as e: return f"Code Generation Error: {e}", 'codegen-output animate__animated animate__fadeInUp error-output'
@app.callback(Output('summarization-output-display', 'children'), Output('summarization-output-display', 'className'), Input('summarize-button', 'n_clicks'), State('summarize-text-input', 'value'), prevent_initial_call=True)
def summarize_text_dash(n_clicks, text):
if not text: return "Please enter text for summarization.", 'summarization-output animate__animated animate__fadeInUp error-output'
api_url = "/api/v1/summarization"; payload = {"text": text}
try: response = requests.post("http://127.0.0.1:7860" + api_url, json=payload); response.raise_for_status()
content_disposition = response.headers.get('Content-Disposition'); download_filename = 'summary.txt'
if content_disposition: filenames = re.findall('filename="([^"]+)"', content_disposition); if filenames: download_filename = filenames[0]
summary_base64 = base64.b64encode(response.content).decode('utf-8'); download_link = html.A('Download Summary', href=f'data:text/plain;base64,{summary_base64}', download=download_filename, className='download-link'); return download_link, 'summarization-output animate__animated animate__fadeInUp success-output'
except requests.exceptions.RequestException as e: return f"Summarization Error: {e}", 'summarization-output animate__animated animate__fadeInUp error-output'
@app.callback(Output('sadtalker-video-output', 'children'), Output('sadtalker-video-output', 'className'), Input('upload-sadtalker-image', 'contents'), State('upload-sadtalker-image', 'filename'), Input('upload-sadtalker-audio', 'contents'), State('upload-sadtalker-audio', 'filename'), prevent_initial_call=True)
def process_sadtalker_dash(image_contents, image_filename, audio_contents, audio_filename):
if not image_contents or not audio_contents: return "Please upload both image and audio for SadTalker.", 'sadtalker-output animate__animated animate__fadeInUp error-output'
try:
image_content_type, image_content_string = image_contents.split(','); decoded_image = base64.b64decode(image_content_string); image_io = io.BytesIO(decoded_image)
audio_content_type, audio_content_string = audio_contents.split(','); decoded_audio = base64.b64decode(audio_content_string); audio_io = io.BytesIO(decoded_audio)
files = {'source_image_file': (image_filename, image_io, image_content_type), 'driven_audio_file': (audio_filename, audio_io, audio_content_type)}
response = requests.post("http://127.0.0.1:7860/api/v1/sadtalker", files=files); response.raise_for_status(); data = response.json(); video_url = data.get('video_url')
if video_url: video_base64 = base64.b64encode(requests.get(video_url).content).decode('utf-8'); return html.Video(src=f'data:video/mp4;base64,{video_base64}', controls=True, className='generated-video'), 'sadtalker-output animate__animated animate__fadeInUp success-output'
else: return "SadTalker video generation failed, check backend logs.", 'sadtalker-output animate__animated animate__fadeInUp error-output'
except requests.exceptions.RequestException as e: return f"SadTalker Error: {e}", 'sadtalker-output animate__animated animate__fadeInUp error-output'
if __name__ == '__main__': app.run_server(host='0.0.0.0', port=7861, debug=False)