Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,29 @@
|
|
| 1 |
-
import
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
| 4 |
from typing import Generator
|
| 5 |
|
| 6 |
-
# Initialize
|
|
|
|
|
|
|
|
|
|
| 7 |
client = InferenceClient(model="Qwen/Qwen2.5-1.5B-Instruct", token=os.getenv("HF_TOKEN"))
|
| 8 |
|
| 9 |
def generate_code_and_explanation(prompt: str) -> Generator[str, None, None]:
|
| 10 |
"""
|
| 11 |
-
Generates code
|
| 12 |
"""
|
| 13 |
system_prompt = (
|
| 14 |
"You are a coding assistant like Grok. Given a user prompt, generate the requested code "
|
| 15 |
-
"and provide a clear explanation. Stream the output line by line.
|
| 16 |
-
"and explanations in plain text with bullet points."
|
| 17 |
)
|
|
|
|
| 18 |
messages = [
|
| 19 |
{"role": "system", "content": system_prompt},
|
| 20 |
{"role": "user", "content": prompt}
|
| 21 |
]
|
| 22 |
|
| 23 |
-
# Stream output from Inference API using chat_completion
|
| 24 |
for chunk in client.chat_completion(
|
| 25 |
messages=messages,
|
| 26 |
max_tokens=3000,
|
|
@@ -28,33 +31,24 @@ def generate_code_and_explanation(prompt: str) -> Generator[str, None, None]:
|
|
| 28 |
top_p=0.9,
|
| 29 |
stream=True
|
| 30 |
):
|
| 31 |
-
# Extract content from the chunk
|
| 32 |
content = chunk.choices[0].delta.content
|
| 33 |
-
if content:
|
| 34 |
yield content
|
| 35 |
|
| 36 |
-
|
| 37 |
-
def
|
| 38 |
"""
|
| 39 |
-
|
| 40 |
"""
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
prompt_input = gr.Textbox(label="Enter your coding prompt", placeholder="e.g., Write a Python function to calculate Fibonacci numbers")
|
| 50 |
-
output_display = gr.Textbox(label="Generated Code and Explanation", interactive=False, lines=10)
|
| 51 |
-
submit_button = gr.Button("Generate")
|
| 52 |
-
|
| 53 |
-
submit_button.click(
|
| 54 |
-
fn=live_code_generator,
|
| 55 |
-
inputs=prompt_input,
|
| 56 |
-
outputs=output_display
|
| 57 |
-
)
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, Response
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
| 4 |
from typing import Generator
|
| 5 |
|
| 6 |
+
# Initialize Flask
|
| 7 |
+
app = Flask(__name__)
|
| 8 |
+
|
| 9 |
+
# Initialize Hugging Face client
|
| 10 |
client = InferenceClient(model="Qwen/Qwen2.5-1.5B-Instruct", token=os.getenv("HF_TOKEN"))
|
| 11 |
|
| 12 |
def generate_code_and_explanation(prompt: str) -> Generator[str, None, None]:
|
| 13 |
"""
|
| 14 |
+
Generates code + explanation with streaming from HF model.
|
| 15 |
"""
|
| 16 |
system_prompt = (
|
| 17 |
"You are a coding assistant like Grok. Given a user prompt, generate the requested code "
|
| 18 |
+
"and provide a clear explanation. Stream the output line by line. "
|
| 19 |
+
"Format code in ```python blocks and explanations in plain text with bullet points."
|
| 20 |
)
|
| 21 |
+
|
| 22 |
messages = [
|
| 23 |
{"role": "system", "content": system_prompt},
|
| 24 |
{"role": "user", "content": prompt}
|
| 25 |
]
|
| 26 |
|
|
|
|
| 27 |
for chunk in client.chat_completion(
|
| 28 |
messages=messages,
|
| 29 |
max_tokens=3000,
|
|
|
|
| 31 |
top_p=0.9,
|
| 32 |
stream=True
|
| 33 |
):
|
|
|
|
| 34 |
content = chunk.choices[0].delta.content
|
| 35 |
+
if content:
|
| 36 |
yield content
|
| 37 |
|
| 38 |
+
@app.route("/generate", methods=["POST"])
|
| 39 |
+
def generate():
|
| 40 |
"""
|
| 41 |
+
Flask endpoint to generate code from user prompt.
|
| 42 |
"""
|
| 43 |
+
data = request.json
|
| 44 |
+
prompt = data.get("prompt", "")
|
| 45 |
+
|
| 46 |
+
def event_stream():
|
| 47 |
+
for chunk in generate_code_and_explanation(prompt):
|
| 48 |
+
yield chunk
|
| 49 |
+
|
| 50 |
+
return Response(event_stream(), mimetype="text/plain")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
if __name__ == "__main__":
|
| 53 |
+
# Run Flask (Hugging Face Spaces will expose this as API)
|
| 54 |
+
app.run(host="0.0.0.0", port=7860)
|