Update app.py
Browse files
app.py
CHANGED
|
@@ -1,54 +1,75 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
|
| 4 |
-
import
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 9 |
-
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.bfloat16, device_map="auto")
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)
|
| 19 |
-
|
| 20 |
-
# Set up streamer for live typing
|
| 21 |
-
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 22 |
-
|
| 23 |
-
# Generation kwargs
|
| 24 |
-
generation_kwargs = {
|
| 25 |
-
"inputs": inputs,
|
| 26 |
-
"streamer": streamer,
|
| 27 |
-
"max_new_tokens": 256,
|
| 28 |
-
"do_sample": True,
|
| 29 |
-
"top_p": 0.95,
|
| 30 |
-
"temperature": 0.7,
|
| 31 |
-
}
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
# Create Gradio
|
| 46 |
-
demo = gr.
|
| 47 |
-
fn=
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
)
|
| 53 |
|
| 54 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import os
|
| 4 |
+
import shutil
|
| 5 |
|
| 6 |
+
# Set HF_HOME to persistent storage to avoid temporary storage bloat
|
| 7 |
+
os.environ["HF_HOME"] = "/data/.huggingface"
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# Load open-source model for code generation (no token required)
|
| 10 |
+
code_generator = pipeline("text-generation", model="bigcode/starcoder")
|
| 11 |
+
|
| 12 |
+
def generate_code_and_response(prompt):
|
| 13 |
+
# Generate code using the model
|
| 14 |
+
code_prompt = f"Generate Python code for: {prompt}\n```python\n"
|
| 15 |
+
code_output = code_generator(code_prompt, max_length=300, num_return_sequences=1, do_sample=True, temperature=0.7)[0]['generated_text']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
# Extract the code part (between ```python and ```)
|
| 18 |
+
code_start = code_output.find("```python") + 9
|
| 19 |
+
code_end = code_output.find("```", code_start)
|
| 20 |
+
code = code_output[code_start:code_end].strip() if code_end != -1 else code_output.strip()
|
| 21 |
+
|
| 22 |
+
# Generate conversational response in Grok-like style
|
| 23 |
+
response_text = (
|
| 24 |
+
f"Alright, I’ve spun up some Python code for your request: '{prompt}'. Here it comes:\n\n"
|
| 25 |
+
f"```python\n{code}\n```\n\n"
|
| 26 |
+
f"Pretty cool, right? I’m running on an open-source model, zipping along on Hugging Face’s free tier. "
|
| 27 |
+
f"Want me to tweak this code, explain it, or whip up something else? I’m all cosmic curiosity!"
|
| 28 |
+
)
|
| 29 |
|
| 30 |
+
# Clean up cache to avoid storage limit issues
|
| 31 |
+
cache_dir = os.path.join("/data/.huggingface", "hub")
|
| 32 |
+
if os.path.exists(cache_dir):
|
| 33 |
+
shutil.rmtree(cache_dir, ignore_errors=True)
|
| 34 |
+
|
| 35 |
+
# Escape special characters for JavaScript
|
| 36 |
+
escaped_response = response_text.replace("`", "\\`").replace("\n", "\\n")
|
| 37 |
|
| 38 |
+
# Wrap response in HTML with JavaScript for typing effect
|
| 39 |
+
html_response = """
|
| 40 |
+
<div id="typing-output" style="font-family: Arial, sans-serif; font-size: 16px; white-space: pre-wrap;"></div>
|
| 41 |
+
<script>
|
| 42 |
+
const text = '""" + escaped_response + """';
|
| 43 |
+
let index = 0;
|
| 44 |
+
const speed = 30; // Typing speed in milliseconds
|
| 45 |
+
const outputElement = document.getElementById('typing-output');
|
| 46 |
+
|
| 47 |
+
function typeWriter() {
|
| 48 |
+
if (index < text.length) {
|
| 49 |
+
outputElement.innerHTML += text.charAt(index);
|
| 50 |
+
index++;
|
| 51 |
+
setTimeout(typeWriter, speed);
|
| 52 |
+
}
|
| 53 |
+
}
|
| 54 |
+
typeWriter();
|
| 55 |
+
</script>
|
| 56 |
+
"""
|
| 57 |
+
return html_response
|
| 58 |
|
| 59 |
+
# Create Gradio interface
|
| 60 |
+
demo = gr.Interface(
|
| 61 |
+
fn=generate_code_and_response,
|
| 62 |
+
inputs=gr.Textbox(label="Enter your coding request", placeholder="e.g., Create a function to calculate factorial"),
|
| 63 |
+
outputs=gr.HTML(label="Generated Code and Response"),
|
| 64 |
+
title="Live CodeGen Bot",
|
| 65 |
+
description="Generate Python code using open-source models with a Grok-like conversational style and live typing effect. Hosted on Hugging Face Spaces free tier.",
|
| 66 |
+
css="""
|
| 67 |
+
body { background-color: #f0f2f5; }
|
| 68 |
+
.gr-box { border-radius: 8px; padding: 20px; }
|
| 69 |
+
#typing-output { background: #fff; padding: 15px; border: 1px solid #ddd; border-radius: 5px; }
|
| 70 |
+
"""
|
| 71 |
)
|
| 72 |
|
| 73 |
+
# Launch the app
|
| 74 |
+
if __name__ == "__main__":
|
| 75 |
+
demo.launch()
|