Spaces:
Running
Running
from main import * | |
from tts_api import tts_api as tts_module_api | |
from stt_api import stt_api as stt_module_api | |
from sentiment_api import sentiment_api as sentiment_module_api | |
from imagegen_api import imagegen_api as imagegen_module_api | |
from musicgen_api import musicgen_api as musicgen_module_api | |
from translation_api import translation_api as translation_module_api | |
from codegen_api import codegen_api as codegen_module_api | |
from text_to_video_api import text_to_video_api as text_to_video_module_api | |
from summarization_api import summarization_api as summarization_module_api | |
from image_to_3d_api import image_to_3d_api as image_to_3d_module_api | |
from xtts_api import xtts_api as xtts_module_api | |
from flask import Flask, request, jsonify, Response, send_file, stream_with_context | |
from flask_cors import CORS | |
import io | |
import queue | |
import base64 | |
import gradio as gr | |
app = Flask(__name__) | |
CORS(app) | |
html_code = """<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>AI Text Generation</title> | |
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css"/> | |
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" integrity="sha512-9usAa10IRO0HhonpyAIVpjrylPvoDwiPUiKdWk5t3PyolY1cOd4DSE0Ga+ri4AuTroPR5aQvXU9xC6qOPnzFeg==" crossorigin="anonymous" referrerpolicy="no-referrer" /> | |
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script> | |
<style> | |
body { | |
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; | |
background: #f0f0f0; | |
color: #333; | |
margin: 0; | |
padding: 0; | |
display: flex; | |
flex-direction: column; | |
align-items: center; | |
min-height: 100vh; | |
} | |
.container { | |
width: 95%; | |
max-width: 900px; | |
padding: 20px; | |
background-color: #fff; | |
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); | |
border-radius: 8px; | |
margin-top: 20px; | |
margin-bottom: 20px; | |
display: flex; | |
flex-direction: column; | |
} | |
.header { | |
text-align: center; | |
margin-bottom: 20px; | |
} | |
.header h1 { | |
font-size: 2em; | |
color: #333; | |
} | |
.form-group { | |
margin-bottom: 15px; | |
} | |
.form-group textarea { | |
width: 100%; | |
padding: 10px; | |
border: 1px solid #ccc; | |
border-radius: 5px; | |
font-size: 16px; | |
box-sizing: border-box; | |
resize: vertical; | |
} | |
button { | |
padding: 10px 15px; | |
border: none; | |
border-radius: 5px; | |
background-color: #007bff; | |
color: white; | |
font-size: 18px; | |
cursor: pointer; | |
transition: background-color 0.3s ease; | |
} | |
button:hover { | |
background-color: #0056b3; | |
} | |
#output { | |
margin-top: 20px; | |
padding: 15px; | |
border: 1px solid #ddd; | |
border-radius: 5px; | |
background-color: #f9f9f9; | |
white-space: pre-wrap; | |
word-break: break-word; | |
overflow-y: auto; | |
max-height: 100vh; | |
} | |
#output strong { | |
font-weight: bold; | |
} | |
.animated-text { | |
position: fixed; | |
top: 20px; | |
left: 20px; | |
font-size: 1.5em; | |
color: rgba(0, 0, 0, 0.1); | |
pointer-events: none; | |
z-index: -1; | |
} | |
@media (max-width: 768px) { | |
.container { | |
width: 98%; | |
margin-top: 10px; | |
margin-bottom: 10px; | |
padding: 15px; | |
} | |
.header h1 { | |
font-size: 1.8em; | |
} | |
.form-group textarea, .form-group input[type="text"] { | |
font-size: 14px; | |
padding: 8px; | |
} | |
button { | |
font-size: 16px; | |
padding: 8px 12px; | |
} | |
#output { | |
font-size: 14px; | |
padding: 10px; | |
margin-top: 15px; | |
} | |
} | |
</style> | |
</head> | |
<body> | |
<div class="animated-text animate__animated animate__fadeIn animate__infinite infinite">AI POWERED</div> | |
<div class="container"> | |
<div class="header animate__animated animate__fadeInDown"> | |
</div> | |
<div class="form-group animate__animated animate__fadeInLeft"> | |
<textarea id="text" rows="5" placeholder="Enter text"></textarea> | |
</div> | |
<button onclick="generateText()" class="animate__animated animate__fadeInUp">Generate Reasoning</button> | |
<div id="output" class="animate__animated"> | |
<strong>Response:</strong><br> | |
<span id="generatedText"></span> | |
</div> | |
</div> | |
<script> | |
let eventSource = null; | |
let accumulatedText = ""; | |
let lastResponse = ""; | |
async function generateText() { | |
const inputText = document.getElementById("text").value; | |
document.getElementById("generatedText").innerText = ""; | |
accumulatedText = ""; | |
if (eventSource) { | |
eventSource.close(); | |
} | |
const temp = 0.7; | |
const top_k_val = 40; | |
const top_p_val = 0.0; | |
const repetition_penalty_val = 1.2; | |
const requestData = { | |
text: inputText, | |
temp: temp, | |
top_k: top_k_val, | |
top_p: top_p_val, | |
reppenalty: repetition_penalty_val | |
}; | |
const params = new URLSearchParams(requestData).toString(); | |
eventSource = new EventSource('/api/v1/generate_stream?' + params); | |
eventSource.onmessage = function(event) { | |
if (event.data === "<END_STREAM>") { | |
eventSource.close(); | |
const currentResponse = accumulatedText.replace("<|endoftext|>", "").replace(/\s+(?=[.,,。])/g, '').trim(); | |
if (currentResponse === lastResponse.trim()) { | |
accumulatedText = "**Response is repetitive. Please try again or rephrase your query.**"; | |
} else { | |
lastResponse = currentResponse; | |
} | |
document.getElementById("generatedText").innerHTML = marked.parse(accumulatedText); | |
return; | |
} | |
accumulatedText += event.data; | |
let partialText = accumulatedText.replace("<|endoftext|>", "").replace(/\s+(?=[.,,。])/g, '').trim(); | |
document.getElementById("generatedText").innerHTML = marked.parse(partialText); | |
}; | |
eventSource.onerror = function(error) { | |
console.error("SSE error", error); | |
eventSource.close(); | |
}; | |
const outputDiv = document.getElementById("output"); | |
outputDiv.classList.add("show"); | |
} | |
function base64ToBlob(base64Data, contentType) { | |
contentType = contentType || ''; | |
const sliceSize = 1024; | |
const byteCharacters = atob(base64Data); | |
const bytesLength = byteCharacters.length; | |
const slicesCount = Math.ceil(bytesLength / sliceSize); | |
const byteArrays = new Array(slicesCount); | |
for (let sliceIndex = sliceIndex < slicesCount; ++sliceIndex) { | |
const begin = sliceIndex * sliceSize; | |
const end = Math.min(begin + sliceSize, bytesLength); | |
const bytes = new Array(end - begin); | |
for (let offset = begin, i = 0; offset < end; ++i, ++offset) { | |
bytes[i] = byteCharacters[offset].charCodeAt(0); | |
} | |
byteArrays[sliceIndex] = new Uint8Array(bytes); | |
} | |
return new Blob(byteArrays, { type: contentType }); | |
} | |
</script> | |
</body> | |
</html> | |
""" | |
feedback_queue = queue.Queue() | |
def index(): | |
return html_code | |
def generate_stream(): | |
text = request.args.get("text", "") | |
temp = float(request.args.get("temp", 0.7)) | |
top_k = int(request.args.get("top_k", 40)) | |
top_p = float(request.args.get("top_p", 0.0)) | |
reppenalty = float(request.args.get("reppenalty", 1.2)) | |
response_queue = queue.Queue() | |
reasoning_queue.put({ | |
'text_input': text, | |
'temperature': temp, | |
'top_k': top_k, | |
'top_p': top_p, | |
'repetition_penalty': reppenalty, | |
'response_queue': response_queue | |
}) | |
def event_stream(): | |
while True: | |
output = response_queue.get() | |
if "error" in output: | |
yield "data: <ERROR>\n\n" | |
break | |
text_chunk = output.get("text") | |
if text_chunk: | |
for word in text_chunk.split(' '): | |
clean_word = word.strip() | |
if clean_word: | |
yield "data: " + clean_word + "\n\n" | |
yield "data: <END_STREAM>\n\n" | |
break | |
return Response(event_stream(), mimetype="text/event-stream") | |
def generate(): | |
data = request.get_json() | |
text = data.get("text", "") | |
temp = float(data.get("temp", 0.7)) | |
top_k = int(data.get("top_k", 40)) | |
top_p = float(data.get("top_p", 0.0)) | |
reppenalty = float(data.get("reppenalty", 1.2)) | |
response_queue = queue.Queue() | |
reasoning_queue.put({ | |
'text_input': text, | |
'temperature': temp, | |
'top_k': top_k, | |
'top_p': top_p, | |
'repetition_penalty': reppenalty, | |
'response_queue': response_queue | |
}) | |
output = response_queue.get() | |
if "error" in output: | |
return jsonify({"error": output["error"]}), 500 | |
result_text = output.get("text", "").strip() | |
return jsonify({"response": result_text}) | |
def feedback(): | |
data = request.get_json() | |
feedback_text = data.get("feedback_text") | |
correct_category = data.get("correct_category") | |
if feedback_text and correct_category: | |
feedback_queue.put((feedback_text, correct_category)) | |
return jsonify({"status": "feedback received"}) | |
return jsonify({"status": "feedback failed"}), 400 | |
def tts_api(): | |
return tts_module_api() | |
def stt_api(): | |
return stt_module_api() | |
def sentiment_api(): | |
return sentiment_module_api() | |
def imagegen_api(): | |
return imagegen_module_api() | |
def musicgen_api(): | |
return musicgen_module_api() | |
def translation_api(): | |
return translation_module_api() | |
def codegen_api(): | |
return codegen_module_api() | |
def text_to_video_api(): | |
return text_to_video_module_api() | |
def summarization_api(): | |
return summarization_module_api() | |
def image_to_3d_api(): | |
return image_to_3d_module_api() | |
def xtts_clone_api(): | |
return xtts_module_api() | |
def sadtalker(): | |
from sadtalker_api import router as sadtalker_router | |
return sadtalker_router.create_video() | |
if __name__ == "__main__": | |
with gr.Blocks() as demo: | |
gr.Markdown("## AI Powerhouse") | |
with gr.Tab("Text Generation"): | |
text_input = gr.Textbox(lines=5, placeholder="Enter text") | |
text_output = gr.Markdown() | |
text_button = gr.Button("Generate Text") | |
text_button.click(generate, inputs=text_input, outputs=text_output) | |
with gr.Tab("Image Generation"): | |
image_text_input = gr.Textbox(lines=3, placeholder="Enter prompt for image") | |
image_output = gr.Image() | |
image_button = gr.Button("Generate Image") | |
image_button.click(imagegen_api, inputs=image_text_input, outputs=image_output) | |
with gr.Tab("Music Generation"): | |
music_text_input = gr.Textbox(lines=3, placeholder="Enter prompt for music") | |
music_output = gr.Audio() | |
music_button = gr.Button("Generate Music") | |
music_button.click(musicgen_api, inputs=music_text_input, outputs=music_output) | |
with gr.Tab("Code Generation"): | |
code_text_input = gr.Textbox(lines=3, placeholder="Enter prompt for code") | |
code_output = gr.File() | |
code_button = gr.Button("Generate Code") | |
code_button.click(codegen_api, inputs=code_text_input, outputs=code_output) | |
with gr.Tab("Text to Video"): | |
video_text_input = gr.Textbox(lines=3, placeholder="Enter prompt for video") | |
video_output = gr.Video() | |
video_button = gr.Button("Generate Video") | |
video_button.click(text_to_video_api, inputs=video_text_input, outputs=video_output) | |
with gr.Tab("Summarization"): | |
summary_text_input = gr.Textbox(lines=5, placeholder="Enter text to summarize") | |
summary_output = gr.Textbox() | |
summary_button = gr.Button("Summarize") | |
summary_button.click(summarization_api, inputs=summary_text_input, outputs=summary_output) | |
with gr.Tab("Translation"): | |
translate_text_input = gr.Textbox(lines=3, placeholder="Enter text to translate") | |
translate_lang_dropdown = gr.Dropdown(['es', 'en', 'fr', 'de'], value='es', label="Target Language") | |
translation_output = gr.Textbox() | |
translate_button = gr.Button("Translate") | |
translate_button.click(translation_api, inputs=[translate_text_input, translate_lang_dropdown], outputs=translation_output) | |
with gr.Tab("Sentiment Analysis"): | |
sentiment_text_input = gr.Textbox(lines=3, placeholder="Enter text for sentiment analysis") | |
sentiment_output = gr.Textbox() | |
sentiment_button = gr.Button("Analyze Sentiment") | |
sentiment_button.click(sentiment_api, inputs=sentiment_text_input, outputs=sentiment_output) | |
with gr.Tab("Text to Speech"): | |
tts_text_input = gr.Textbox(lines=3, placeholder="Enter text for speech") | |
tts_output = gr.Audio() | |
tts_button = gr.Button("Generate Speech") | |
tts_button.click(tts_api, inputs=tts_text_input, outputs=tts_output) | |
with gr.Tab("Voice Cloning (XTTS)"): | |
xtts_text_input = gr.Textbox(lines=3, placeholder="Enter text for voice cloning") | |
xtts_audio_input = gr.Audio(source="upload", type="filepath", label="Reference Audio for Voice Cloning") | |
xtts_output = gr.Audio() | |
xtts_button = gr.Button("Clone Voice") | |
xtts_button.click(xtts_module_api, inputs=[xtts_text_input, xtts_audio_input], outputs=xtts_output) | |
with gr.Tab("Speech to Text"): | |
stt_audio_input = gr.Audio(source="microphone", type="filepath") | |
stt_output = gr.Textbox() | |
stt_button = gr.Button("Transcribe Speech") | |
stt_button.click(stt_api, inputs=stt_audio_input, outputs=stt_output) | |
with gr.Tab("Image to 3D"): | |
image_3d_input = gr.Image(source="upload", type="filepath") | |
model_3d_output = gr.File() | |
image_3d_button = gr.Button("Generate 3D Model") | |
image_3d_button.click(image_to_3d_api, inputs=image_3d_input, outputs=model_3d_output) | |
app = gr.routes.App(demo) | |
app.run(host="0.0.0.0", port=7860) | |