akhaliq HF Staff commited on
Commit
a508c6b
·
verified ·
1 Parent(s): 7bfe48a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +117 -123
app.py CHANGED
@@ -1,137 +1,131 @@
1
  import gradio as gr
2
- import torch
3
  from transformers import pipeline
4
 
5
- def initialize_model():
6
- """Initialize the text generation pipeline with device detection"""
7
- # Check if CUDA is available, otherwise fall back to CPU
8
- device = "cuda" if torch.cuda.is_available() else "cpu"
9
- print(f"Using device: {device}")
10
-
11
- try:
12
- generator = pipeline(
13
- "text-generation",
14
- model="akhaliq/MyGemmaGradioCoder",
15
- device=device
16
- )
17
- return generator
18
- except Exception as e:
19
- print(f"Error loading model: {e}")
20
- # Fallback to CPU if CUDA fails
21
- if device == "cuda":
22
- print("Falling back to CPU...")
23
- generator = pipeline(
24
- "text-generation",
25
- model="akhaliq/gemma-3-270m-gradio-coder",
26
- device="cpu"
27
- )
28
- return generator
29
- raise e
30
-
31
- # Initialize the model globally
32
- print("Loading model...")
33
- generator = initialize_model()
34
- print("Model loaded successfully!")
35
 
36
- def chat_response(message, history):
37
- """Generate response for the chatbot"""
38
- try:
39
- # Format the message for the model
40
- input_message = [{"role": "user", "content": message}]
41
-
42
- # Generate response
43
- output = generator(
44
- input_message,
45
- max_new_tokens=20000,
46
- return_full_text=False,
47
- do_sample=True,
48
- temperature=0.7,
49
- pad_token_id=generator.tokenizer.eos_token_id
50
- )[0]
51
-
52
- response = output["generated_text"]
53
- return response
54
-
55
- except Exception as e:
56
- return f"Sorry, I encountered an error: {str(e)}"
57
-
58
- # Create the Gradio interface
59
- def create_chatbot():
60
- """Create and launch the Gradio chatbot interface"""
61
-
62
- # Custom CSS for better styling
63
- css = """
64
- .gradio-container {
65
- max-width: 800px !important;
66
- margin: auto !important;
67
- }
68
- .chat-message {
69
- padding: 10px !important;
70
- margin: 5px !important;
71
- border-radius: 10px !important;
72
- }
73
  """
 
 
 
 
 
74
 
75
- # Create the chatbot interface
76
- with gr.Blocks(css=css, title="AI Chatbot") as demo:
77
- gr.Markdown("# 🤖 AI Chatbot")
78
- gr.Markdown("*Powered by Gemma-3-270m model via Transformers*")
79
-
80
- chatbot = gr.Chatbot(
81
- height=500,
82
- bubble_full_width=False,
83
- show_label=False
84
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
 
86
- with gr.Row():
87
- msg = gr.Textbox(
88
- placeholder="Type your message here...",
89
- show_label=False,
90
- scale=4
 
 
 
 
 
 
 
 
91
  )
92
- send_btn = gr.Button("Send", scale=1, variant="primary")
93
- clear_btn = gr.Button("Clear", scale=1)
94
-
95
- # Example questions
96
- gr.Examples(
97
- examples=[
98
- "If you had a time machine, but could only go to the past or the future once and never return, which would you choose and why?",
99
- "What's the most important lesson you've learned in life?",
100
- "How do you think AI will change the world in the next 10 years?",
101
- "What would you do if you had unlimited resources for one day?"
102
- ],
103
- inputs=msg
104
- )
105
-
106
- def respond(message, chat_history):
107
- if not message.strip():
108
- return chat_history, ""
109
 
110
- # Get bot response
111
- bot_message = chat_response(message, chat_history)
 
112
 
113
- # Add to chat history
114
- chat_history.append((message, bot_message))
115
- return chat_history, ""
116
-
117
- def clear_chat():
118
- return [], ""
 
 
 
 
 
 
119
 
120
- # Event handlers
121
- msg.submit(respond, [msg, chatbot], [chatbot, msg])
122
- send_btn.click(respond, [msg, chatbot], [chatbot, msg])
123
- clear_btn.click(clear_chat, None, [chatbot, msg])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
 
125
- return demo
126
-
127
- if __name__ == "__main__":
128
- print("Creating Gradio interface...")
129
- demo = create_chatbot()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
 
131
- print("Starting Gradio server...")
132
- demo.launch(
133
- share=False, # Set to True if you want a public link
134
- server_name="0.0.0.0", # Allow external connections
135
- server_port=7860,
136
- show_error=True
 
 
 
 
 
137
  )
 
 
 
 
 
1
  import gradio as gr
 
2
  from transformers import pipeline
3
 
4
+ # Initialize the text generation pipeline
5
+ pipe = pipeline("text-generation", model="akhaliq/MyGemmaGradioCoder")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
+ def generate_code(user_input):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  """
9
+ Generate code based on user input using the Gemma model
10
+ """
11
+ messages = [
12
+ {"role": "user", "content": user_input},
13
+ ]
14
 
15
+ # Generate response from the model
16
+ response = pipe(messages, max_new_tokens=512, temperature=0.7, do_sample=True)
17
+
18
+ # Extract the generated text from the response
19
+ generated_text = response[0]['generated_text']
20
+
21
+ # If the response contains the full conversation, extract just the assistant's response
22
+ if isinstance(generated_text, list):
23
+ # Handle conversation format
24
+ for msg in generated_text:
25
+ if msg.get('role') == 'assistant':
26
+ return msg.get('content', '')
27
+ # If no assistant message found, return the last message content
28
+ return generated_text[-1].get('content', '') if generated_text else ""
29
+ else:
30
+ # Handle string format - try to extract the code after the user input
31
+ if user_input in generated_text:
32
+ return generated_text.split(user_input)[-1].strip()
33
+ return generated_text
34
+
35
+ # Create the Gradio interface
36
+ with gr.Blocks(title="Text to Code Generator", theme=gr.themes.Soft()) as demo:
37
+ gr.Markdown(
38
+ """
39
+ # 🚀 Text to Code Generator
40
 
41
+ Generate code from natural language descriptions using the Gemma Gradio Coder model.
42
+ Simply describe what you want to build, and the AI will generate the corresponding code!
43
+ """
44
+ )
45
+
46
+ with gr.Row():
47
+ with gr.Column(scale=1):
48
+ # Input section
49
+ input_text = gr.Textbox(
50
+ label="Describe what you want to code",
51
+ placeholder="e.g., Create a Python function that calculates the factorial of a number",
52
+ lines=5,
53
+ max_lines=10
54
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
+ with gr.Row():
57
+ generate_btn = gr.Button("Generate Code", variant="primary", scale=2)
58
+ clear_btn = gr.ClearButton([input_text], value="Clear", scale=1)
59
 
60
+ # Examples section
61
+ gr.Examples(
62
+ examples=[
63
+ ["Create a Python function to check if a number is prime"],
64
+ ["Write a JavaScript function to reverse a string"],
65
+ ["Create a React component for a todo list item"],
66
+ ["Write a SQL query to find the top 5 customers by total purchase amount"],
67
+ ["Create a Python class for a bank account with deposit and withdraw methods"],
68
+ ],
69
+ inputs=input_text,
70
+ label="Example Prompts"
71
+ )
72
 
73
+ with gr.Column(scale=1):
74
+ # Output section
75
+ output_code = gr.Code(
76
+ label="Generated Code",
77
+ language="python",
78
+ lines=20,
79
+ interactive=True,
80
+ show_line_numbers=True,
81
+ wrap_lines=True,
82
+ autocomplete=True
83
+ )
84
+
85
+ with gr.Row():
86
+ copy_btn = gr.Button("📋 Copy Code", scale=1)
87
+
88
+ # Add event handlers
89
+ generate_btn.click(
90
+ fn=generate_code,
91
+ inputs=input_text,
92
+ outputs=output_code,
93
+ api_name="generate"
94
+ )
95
 
96
+ input_text.submit(
97
+ fn=generate_code,
98
+ inputs=input_text,
99
+ outputs=output_code
100
+ )
101
+
102
+ # Copy functionality (using JavaScript)
103
+ copy_btn.click(
104
+ fn=None,
105
+ inputs=output_code,
106
+ outputs=None,
107
+ js="""
108
+ (code) => {
109
+ navigator.clipboard.writeText(code);
110
+ alert('Code copied to clipboard!');
111
+ return null;
112
+ }
113
+ """
114
+ )
115
 
116
+ # Footer
117
+ gr.Markdown(
118
+ """
119
+ ---
120
+ 💡 **Tips:**
121
+ - Be specific about the programming language you want
122
+ - Include details about inputs, outputs, and edge cases
123
+ - You can edit the generated code directly in the output box
124
+
125
+ Powered by [akhaliq/MyGemmaGradioCoder](https://huggingface.co/akhaliq/MyGemmaGradioCoder)
126
+ """
127
  )
128
+
129
+ # Launch the app
130
+ if __name__ == "__main__":
131
+ demo.launch()