Invescoz commited on
Commit
c800a14
·
verified ·
1 Parent(s): 9811777

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -30
app.py CHANGED
@@ -1,26 +1,29 @@
1
- import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  import os
4
  from typing import Generator
5
 
6
- # Initialize Inference API client
 
 
 
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 and explanation using Qwen2.5-1.5B-Instruct via Inference API with streaming.
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. Format code in ```python blocks "
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: # Only yield non-empty content
34
  yield content
35
 
36
- # Gradio interface with streaming
37
- def live_code_generator(prompt: str):
38
  """
39
- Handles streaming output for the Gradio interface.
40
  """
41
- output = ""
42
- for chunk in generate_code_and_explanation(prompt):
43
- output += chunk
44
- yield output
45
-
46
- # Gradio app
47
- with gr.Blocks() as demo:
48
- gr.Markdown("# Invescoz AI Studio: Live Code Generator")
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
- # Launch the app (handled by Hugging Face Spaces)
60
- demo.launch()
 
 
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)