Invescoz commited on
Commit
dae8e37
·
verified ·
1 Parent(s): 8f3355d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -56
app.py CHANGED
@@ -1,65 +1,54 @@
1
  import gradio as gr
2
- from transformers import pipeline
 
 
3
 
4
- # Load open-source model for code generation (no token required)
5
- code_generator = pipeline("text-generation", model="bigcode/starcoder")
 
 
6
 
7
- def generate_code_and_response(prompt):
8
- # Generate code using the model
9
- code_prompt = f"Generate Python code for: {prompt}\n```python\n"
10
- code_output = code_generator(code_prompt, max_length=500, num_return_sequences=1, do_sample=True, temperature=0.7)[0]['generated_text']
 
11
 
12
- # Extract the code part (between ```python and ```)
13
- code_start = code_output.find("```python") + 9
14
- code_end = code_output.find("```", code_start)
15
- code = code_output[code_start:code_end].strip() if code_end != -1 else code_output.strip()
16
-
17
- # Generate conversational response in Grok-like style
18
- response_text = (
19
- f"Alright, I’ve spun up some Python code for your request: '{prompt}'. Here it comes:\n\n"
20
- f"```python\n{code}\n```\n\n"
21
- f"Pretty cool, right? I’m running on an open-source model, zipping along on Hugging Face’s free tier. "
22
- f"Want me to tweak this code, explain it, or whip up something else? I’m all cosmic curiosity!"
23
- )
 
 
 
 
 
 
 
24
 
25
- # Escape special characters for JavaScript
26
- escaped_response = response_text.replace("`", "\\`").replace("\n", "\\n")
 
 
 
27
 
28
- # Wrap response in HTML with JavaScript for typing effect
29
- html_response = """
30
- <div id="typing-output" style="font-family: Arial, sans-serif; font-size: 16px; white-space: pre-wrap;"></div>
31
- <script>
32
- const text = '""" + escaped_response + """';
33
- let index = 0;
34
- const speed = 30; // Typing speed in milliseconds
35
- const outputElement = document.getElementById('typing-output');
36
-
37
- function typeWriter() {
38
- if (index < text.length) {
39
- outputElement.innerHTML += text.charAt(index);
40
- index++;
41
- setTimeout(typeWriter, speed);
42
- }
43
- }
44
- typeWriter();
45
- </script>
46
- """
47
- return html_response
48
 
49
- # Create Gradio interface
50
- demo = gr.Interface(
51
- fn=generate_code_and_response,
52
- inputs=gr.Textbox(label="Enter your coding request", placeholder="e.g., Create a function to calculate factorial"),
53
- outputs=gr.HTML(label="Generated Code and Response"),
54
- title="Live CodeGen Bot",
55
- 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.",
56
- css="""
57
- body { background-color: #f0f2f5; }
58
- .gr-box { border-radius: 8px; padding: 20px; }
59
- #typing-output { background: #fff; padding: 15px; border: 1px solid #ddd; border-radius: 5px; }
60
- """
61
  )
62
 
63
- # Launch the app
64
- if __name__ == "__main__":
65
- demo.launch()
 
1
  import gradio as gr
2
+ from threading import Thread
3
+ from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer
4
+ import torch
5
 
6
+ # Load non-gated model and tokenizer
7
+ model_id = "Qwen/Qwen2-7B-Instruct"
8
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
9
+ model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.bfloat16, device_map="auto")
10
 
11
+ def chat(message, history):
12
+ # Build message history
13
+ messages = [
14
+ {"role": "system", "content": "You are a helpful and friendly assistant."}
15
+ ] + history + [{"role": "user", "content": message}]
16
 
17
+ # Prepare inputs
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
+ # Run generation in a separate thread
34
+ thread = Thread(target=model.generate, kwargs=generation_kwargs)
35
+ thread.start()
36
 
37
+ # Yield tokens for live streaming
38
+ generated_text = ""
39
+ for new_text in streamer:
40
+ generated_text += new_text
41
+ yield generated_text
42
 
43
+ thread.join()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
+ # Create Gradio chat interface
46
+ demo = gr.ChatInterface(
47
+ fn=chat,
48
+ type="messages",
49
+ title="Qwen2-7B Chatbot",
50
+ description="Chat with a non-gated Qwen2-7B-Instruct model. Responses stream live.",
51
+ examples=[["Tell me a fun fact."], ["Explain neural networks in simple terms."]],
 
 
 
 
 
52
  )
53
 
54
+ demo.launch()