Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,39 +1,18 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import pipeline, set_seed
|
| 3 |
import os
|
| 4 |
-
import
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
# Initialize the pipeline globally. This runs once when the Space starts.
|
| 11 |
-
# Note: This step requires significant resources and will make the initial load SLOW on the free CPU tier.
|
| 12 |
-
try:
|
| 13 |
-
generator = pipeline(
|
| 14 |
-
"text-generation",
|
| 15 |
-
model=MODEL_ID,
|
| 16 |
-
trust_remote_code=True,
|
| 17 |
-
device_map="auto" # Use available resources (CPU in the free tier)
|
| 18 |
-
)
|
| 19 |
-
# Set a seed for reproducibility (optional)
|
| 20 |
-
set_seed(42)
|
| 21 |
-
except Exception as e:
|
| 22 |
-
# Define a safe fallback pipeline if the model load fails
|
| 23 |
-
print(f"Error loading LLM: {e}")
|
| 24 |
-
generator = None
|
| 25 |
-
|
| 26 |
-
# --- 1. Define the Core Logic Functions ---
|
| 27 |
|
| 28 |
def generate_output(project_name, user_input, file_path=None):
|
| 29 |
"""Generates specialized output using the LLM based on the project context."""
|
| 30 |
-
if not generator:
|
| 31 |
-
return "[ERROR] The LLM failed to initialize. The Hugging Face Space may have timed out or run out of memory. Please try again in a few minutes."
|
| 32 |
|
| 33 |
if not user_input and project_name != "VYZENVO":
|
| 34 |
return "Please provide text input for the AI to process."
|
| 35 |
|
| 36 |
-
#
|
| 37 |
if project_name == "VYZVERSE":
|
| 38 |
system_prompt = (
|
| 39 |
"You are Commander Zylos, the lead narrative designer for the VYZVERSE animation project. "
|
|
@@ -46,16 +25,15 @@ def generate_output(project_name, user_input, file_path=None):
|
|
| 46 |
system_prompt = (
|
| 47 |
"You are a senior game code debugger and architect specializing in Python and C#. Analyze the "
|
| 48 |
"provided code and bug description. Suggest the most efficient fix or generate the requested starter function. "
|
| 49 |
-
"Always output the resulting code or fix inside a clear code block.
|
| 50 |
)
|
| 51 |
max_tokens = 512
|
| 52 |
|
| 53 |
elif project_name == "VYZENVO":
|
| 54 |
-
# Simulate RAG by including the document content in the prompt
|
| 55 |
document_content = ""
|
| 56 |
if file_path and os.path.exists(file_path):
|
| 57 |
-
with open(file_path, 'r', encoding='utf-8') as f:
|
| 58 |
-
# Read only the first 2000 characters to avoid exceeding the model's 8k token limit
|
| 59 |
document_content = f.read(2000)
|
| 60 |
|
| 61 |
system_prompt = (
|
|
@@ -73,71 +51,87 @@ def generate_output(project_name, user_input, file_path=None):
|
|
| 73 |
system_prompt = "You are a general-purpose AI assistant. Respond helpfully."
|
| 74 |
max_tokens = 250
|
| 75 |
|
| 76 |
-
#
|
| 77 |
-
prompt = f"<s>[INST] {system_prompt}\n\n{user_input} [/INST]"
|
| 78 |
-
|
| 79 |
-
# 3. Call the LLM pipeline
|
| 80 |
try:
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
| 88 |
)
|
| 89 |
|
| 90 |
-
generated_text = response[0]
|
| 91 |
-
|
| 92 |
-
# Clean up the output if the model re-prints the instruction (common issue)
|
| 93 |
-
if generated_text.startswith(system_prompt):
|
| 94 |
-
generated_text = generated_text[len(system_prompt):].strip()
|
| 95 |
-
|
| 96 |
return generated_text
|
| 97 |
|
| 98 |
except Exception as e:
|
| 99 |
-
return f"ERROR: Inference failed.
|
| 100 |
|
| 101 |
|
| 102 |
-
#
|
| 103 |
-
with gr.Blocks(title="VYZ Interstellar Multi-Tool AI") as demo:
|
| 104 |
gr.Markdown("# 🚀 VYZ Interstellar AI Assistant")
|
| 105 |
-
gr.Markdown("A specialized, multi-tool assistant for VYZVERSE, ReVYZGen, and VYZENVO projects. **(Powered by Mistral 7B
|
| 106 |
|
| 107 |
with gr.Tabs() as tabs:
|
| 108 |
-
#
|
| 109 |
with gr.TabItem("VYZVERSE: Script & Lore", id="vyzverse_tab"):
|
| 110 |
gr.Markdown("### Generate Dialogue, Descriptions, or Scene Synopses.")
|
| 111 |
-
vyzverse_input = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
vyzverse_output = gr.Textbox(label="VYZVERSE Generation", lines=10, interactive=False)
|
| 113 |
vyzverse_btn = gr.Button("Generate VYZVERSE Content", variant="primary")
|
| 114 |
|
| 115 |
vyzverse_btn.click(
|
| 116 |
-
fn=generate_output,
|
| 117 |
-
inputs=
|
| 118 |
outputs=vyzverse_output
|
| 119 |
)
|
| 120 |
|
| 121 |
-
#
|
| 122 |
with gr.TabItem("ReVYZGen: Code Helper", id="revyzgen_tab"):
|
| 123 |
gr.Markdown("### Debug Code Snippets or Generate Starter Functions.")
|
| 124 |
-
revyzgen_input = gr.Code(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
revyzgen_output = gr.Code(label="Corrected Code / Suggestion", language="python", lines=10, interactive=False)
|
| 126 |
revyzgen_btn = gr.Button("Analyze ReVYZGen Code", variant="primary")
|
| 127 |
|
| 128 |
revyzgen_btn.click(
|
| 129 |
-
fn=generate_output,
|
| 130 |
-
inputs=
|
| 131 |
outputs=revyzgen_output
|
| 132 |
)
|
| 133 |
|
| 134 |
-
#
|
| 135 |
with gr.TabItem("VYZENVO: Environmental Analysis", id="vyzenvo_tab"):
|
| 136 |
gr.Markdown("### Analyze Private Documents (Simulated RAG). Upload a document and ask a specific question.")
|
| 137 |
-
vyzenvo_file = gr.File(label="Upload VYZENVO Document (
|
| 138 |
-
vyzenvo_question = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
| 139 |
vyzenvo_output = gr.Textbox(label="Analysis Output", lines=10, interactive=False)
|
| 140 |
vyzenvo_btn = gr.Button("Analyze VYZENVO Data", variant="primary")
|
| 141 |
|
| 142 |
-
|
| 143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import os
|
| 3 |
+
from huggingface_hub import InferenceClient
|
| 4 |
|
| 5 |
+
# Use HF Inference API instead of loading model locally
|
| 6 |
+
# This is FREE and doesn't require GPU/high memory
|
| 7 |
+
client = InferenceClient()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
def generate_output(project_name, user_input, file_path=None):
|
| 10 |
"""Generates specialized output using the LLM based on the project context."""
|
|
|
|
|
|
|
| 11 |
|
| 12 |
if not user_input and project_name != "VYZENVO":
|
| 13 |
return "Please provide text input for the AI to process."
|
| 14 |
|
| 15 |
+
# Define the specialized system prompt for context
|
| 16 |
if project_name == "VYZVERSE":
|
| 17 |
system_prompt = (
|
| 18 |
"You are Commander Zylos, the lead narrative designer for the VYZVERSE animation project. "
|
|
|
|
| 25 |
system_prompt = (
|
| 26 |
"You are a senior game code debugger and architect specializing in Python and C#. Analyze the "
|
| 27 |
"provided code and bug description. Suggest the most efficient fix or generate the requested starter function. "
|
| 28 |
+
"Always output the resulting code or fix inside a clear code block."
|
| 29 |
)
|
| 30 |
max_tokens = 512
|
| 31 |
|
| 32 |
elif project_name == "VYZENVO":
|
| 33 |
+
# Simulate RAG by including the document content in the prompt
|
| 34 |
document_content = ""
|
| 35 |
if file_path and os.path.exists(file_path):
|
| 36 |
+
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
|
|
|
|
| 37 |
document_content = f.read(2000)
|
| 38 |
|
| 39 |
system_prompt = (
|
|
|
|
| 51 |
system_prompt = "You are a general-purpose AI assistant. Respond helpfully."
|
| 52 |
max_tokens = 250
|
| 53 |
|
| 54 |
+
# Call the HF Inference API
|
|
|
|
|
|
|
|
|
|
| 55 |
try:
|
| 56 |
+
messages = [
|
| 57 |
+
{"role": "system", "content": system_prompt},
|
| 58 |
+
{"role": "user", "content": user_input}
|
| 59 |
+
]
|
| 60 |
+
|
| 61 |
+
response = client.chat_completion(
|
| 62 |
+
messages=messages,
|
| 63 |
+
model="mistralai/Mistral-7B-Instruct-v0.3",
|
| 64 |
+
max_tokens=max_tokens,
|
| 65 |
+
temperature=0.7
|
| 66 |
)
|
| 67 |
|
| 68 |
+
generated_text = response.choices[0].message.content.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
return generated_text
|
| 70 |
|
| 71 |
except Exception as e:
|
| 72 |
+
return f"ERROR: Inference failed. Detail: {e}"
|
| 73 |
|
| 74 |
|
| 75 |
+
# Build the Gradio Interface
|
| 76 |
+
with gr.Blocks(title="VYZ Interstellar Multi-Tool AI", theme=gr.themes.Soft()) as demo:
|
| 77 |
gr.Markdown("# 🚀 VYZ Interstellar AI Assistant")
|
| 78 |
+
gr.Markdown("A specialized, multi-tool assistant for VYZVERSE, ReVYZGen, and VYZENVO projects. **(Powered by Mistral 7B via HF Inference API)**")
|
| 79 |
|
| 80 |
with gr.Tabs() as tabs:
|
| 81 |
+
# TAB 1: VYZVERSE (Animation/Lore Generation)
|
| 82 |
with gr.TabItem("VYZVERSE: Script & Lore", id="vyzverse_tab"):
|
| 83 |
gr.Markdown("### Generate Dialogue, Descriptions, or Scene Synopses.")
|
| 84 |
+
vyzverse_input = gr.Textbox(
|
| 85 |
+
lines=5,
|
| 86 |
+
label="Input Prompt / Plot Outline",
|
| 87 |
+
placeholder="e.g., Write a tense dialogue between Commander Zylos and the Regulator about planet-side energy drain."
|
| 88 |
+
)
|
| 89 |
vyzverse_output = gr.Textbox(label="VYZVERSE Generation", lines=10, interactive=False)
|
| 90 |
vyzverse_btn = gr.Button("Generate VYZVERSE Content", variant="primary")
|
| 91 |
|
| 92 |
vyzverse_btn.click(
|
| 93 |
+
fn=lambda x: generate_output("VYZVERSE", x, None),
|
| 94 |
+
inputs=vyzverse_input,
|
| 95 |
outputs=vyzverse_output
|
| 96 |
)
|
| 97 |
|
| 98 |
+
# TAB 2: ReVYZGen (Game Code Assistant)
|
| 99 |
with gr.TabItem("ReVYZGen: Code Helper", id="revyzgen_tab"):
|
| 100 |
gr.Markdown("### Debug Code Snippets or Generate Starter Functions.")
|
| 101 |
+
revyzgen_input = gr.Code(
|
| 102 |
+
label="Paste Code (Python/C#)",
|
| 103 |
+
language="python",
|
| 104 |
+
lines=8,
|
| 105 |
+
value="# Paste your game code snippet and bug description here..."
|
| 106 |
+
)
|
| 107 |
revyzgen_output = gr.Code(label="Corrected Code / Suggestion", language="python", lines=10, interactive=False)
|
| 108 |
revyzgen_btn = gr.Button("Analyze ReVYZGen Code", variant="primary")
|
| 109 |
|
| 110 |
revyzgen_btn.click(
|
| 111 |
+
fn=lambda x: generate_output("ReVYZGen", x, None),
|
| 112 |
+
inputs=revyzgen_input,
|
| 113 |
outputs=revyzgen_output
|
| 114 |
)
|
| 115 |
|
| 116 |
+
# TAB 3: VYZENVO (Document Analysis/RAG Simulator)
|
| 117 |
with gr.TabItem("VYZENVO: Environmental Analysis", id="vyzenvo_tab"):
|
| 118 |
gr.Markdown("### Analyze Private Documents (Simulated RAG). Upload a document and ask a specific question.")
|
| 119 |
+
vyzenvo_file = gr.File(label="Upload VYZENVO Document (TXT)", file_types=[".txt"])
|
| 120 |
+
vyzenvo_question = gr.Textbox(
|
| 121 |
+
label="Question about the Document",
|
| 122 |
+
placeholder="e.g., What are the safety parameters for the 'Auror-1' power core?"
|
| 123 |
+
)
|
| 124 |
vyzenvo_output = gr.Textbox(label="Analysis Output", lines=10, interactive=False)
|
| 125 |
vyzenvo_btn = gr.Button("Analyze VYZENVO Data", variant="primary")
|
| 126 |
|
| 127 |
+
vyzenvo_btn.click(
|
| 128 |
+
fn=lambda f, q: generate_output("VYZENVO", q, f.name if f else None),
|
| 129 |
+
inputs=[vyzenvo_file, vyzenvo_question],
|
| 130 |
+
outputs=vyzenvo_output
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
gr.Markdown("---")
|
| 134 |
+
gr.Markdown("*Built by VYZ Interstellar | Powered by Hugging Face Inference API*")
|
| 135 |
+
|
| 136 |
+
if __name__ == "__main__":
|
| 137 |
+
demo.launch()
|