Spaces:
Runtime error
Runtime error
Commit Β·
1e4764f
1
Parent(s): b9ce40a
first commit
Browse filesFiles changed (4)
- README.md+40-1
- app.pyAdded+236
- config.pyAdded+30
- requirements.txtAdded+3
1# gradio-canvas12345678910111213141516171819202122232425262728293031323334353637383940# Gradio Canvas π€Gradio Canvas is a web application inspired by ChatGPT's Canvas. This project combines the capabilities of Fireworks AI and Instructor to create a seamless code generation experience.Built with:- Llama 3.1 405B via [Fireworks AI](https://fireworks.ai)- [Instructor](https://github.com/instructor-ai/instructor) for structured output parsing- [Gradio](https://github.com/gradio-app/gradio) for the web interface## Getting Started### Prerequisites- Fireworks AI API key### Installation1. Clone the repository:```bashgit clone https://github.com/yourusername/gradio-canvas.gitcd gradio-canvas```2. Install the required dependencies:```bashpip install -r requirements.txt```3. Set up your Fireworks AI API key as an environment variable:```bashexport FIREWORKS_API_KEY=your_api_key_here```### UsageRun the application:```bashgradio app.py```
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236"""app.py"""
# Standard library importsimport jsonfrom typing import Tuple
# Third-party importsimport gradio as grimport instructorfrom fireworks.client import Fireworksfrom pydantic import BaseModel, ValidationError
# Local importsfrom config import ( APP_HEADER, APP_TITLE, FIREWORKS_API_KEY, LLM_MAX_TOKENS, LLM_MODEL, LLM_SYSTEM_PROMPT,)
# Initialize Instructor with the Fireworks clientclient = Fireworks(api_key=FIREWORKS_API_KEY)client = instructor.from_fireworks(client)
# Define response models for feedback and code using Pydanticclass CodeResponse(BaseModel): """Code Response"""
planning: str full_python_code: str commentary: str
def get_llm_responses( user_input: str, conversation: list, current_code: str = None) -> Tuple[list, str, str]: """ Generates feedback and code based on user input using the Instructor LLM.
Args: user_input (str): The input text from the user. conversation (list): The conversation history. current_code (str, optional): Existing code if any.
Returns: Tuple[list, str, str]: A tuple containing updated conversation, generated code, and formatted conversation history. """ try: # Update conversation history with user input conversation.append( { "role": "user", "content": ( user_input if current_code is None else f"{user_input} And here is the existing code: {current_code}" ), } )
# Generate Feedback feedback_resp = client.chat.completions.create( model=LLM_MODEL, response_model=CodeResponse, max_tokens=LLM_MAX_TOKENS, messages=conversation, )
code = feedback_resp.full_python_code
# Update conversation history with assistant response conversation.append( { "role": "assistant", "content": feedback_resp.model_dump_json(), } )
# Format conversation history for display conversation_text = ""
conversation_to_print = conversation[1:]
round_number = ( len(conversation_to_print) // 2 ) # Assuming each round has a user and assistant message
# Add the latest conversation pair to the top if len(conversation_to_print) >= 2: latest_pair = conversation_to_print[-2:] conversation_text += f"## Version {round_number}\n\n" for message in latest_pair: if message["role"] != "system": role = message["role"].capitalize() try: content = json.loads(message["content"]) content = content["commentary"] except: content = message["content"].split( " And here is the existing code:" )[0] if content == "": content = "_User edited the code directly_"
emoji = "π€" if role == "User" else "π€" conversation_text += f"**{emoji} {role}:** {content}\n\n"
# Add the rest of the conversation history for i, message in enumerate(conversation_to_print[:-2]): if message["role"] != "system": if i % 2 == 0: round_number = (len(conversation_to_print) - i) // 2 conversation_text += f"## Version {round_number-1}\n\n"
role = message["role"].capitalize() try: content = json.loads(message["content"]) content = content["commentary"] except: content = message["content"].split( " And here is the existing code:" )[0] if content == "": content = "_User edited the code directly_"
emoji = "π€" if role == "User" else "π€" conversation_text += f"**{emoji} {role}:** {content}\n\n"
return conversation, code, conversation_text
except ValidationError as ve: error_msg = f"Response validation error: {ve}" raise gr.Error(error_msg) except Exception as e: error_msg = f"An error occurred: {e}" raise gr.Error(error_msg)
# Define the Gradio interfacewith gr.Blocks( title=APP_TITLE, theme=gr.themes.Ocean(), fill_width=True, fill_height=True) as demo: gr.HTML(APP_HEADER)
with gr.Row(): with gr.Column(scale=1): conversation_output = gr.Markdown(label="Chat History", height=500)
with gr.Column(scale=2): code_output = gr.Code( label="LLM Generated Code", interactive=True, language="python", lines=30, ) with gr.Row(): add_comments_btn = gr.Button("Add Comments π¬") refactor_btn = gr.Button("Refactor π¨")
with gr.Row(): with gr.Column(scale=9): user_input = gr.Textbox( label="Enter Your Request here", placeholder="Type something here...", lines=2, ) with gr.Column(scale=1): submit_btn = gr.Button("Submit π") reset_btn = gr.Button("Reset π")
# Initialize conversation history with system prompt using Gradio State initial_conversation = [ { "role": "system", "content": LLM_SYSTEM_PROMPT, } ]
conversation_state = gr.State( initial_conversation ) # Define a single State instance
# Define the button click event def on_submit(user_input, conversation, current_code): result = get_llm_responses(user_input, conversation, current_code) return [""] + list(result) # Clear the textbox by returning an empty string
submit_btn.click( fn=on_submit, inputs=[user_input, conversation_state, code_output], outputs=[user_input, conversation_state, code_output, conversation_output], )
def add_comments_fn(conversation, current_code): return on_submit( "Please add more comments to the code. Make it production ready.", conversation, current_code, )
add_comments_btn.click( fn=add_comments_fn, inputs=[conversation_state, code_output], outputs=[user_input, conversation_state, code_output, conversation_output], )
def refactor_fn(conversation, current_code): return on_submit( "Please refactor the code. Make it more efficient.", conversation, current_code, )
refactor_btn.click( fn=refactor_fn, inputs=[conversation_state, code_output], outputs=[user_input, conversation_state, code_output, conversation_output], )
def reset_fn(): return "", initial_conversation, "", ""
reset_btn.click( fn=reset_fn, outputs=[user_input, conversation_state, code_output, conversation_output], )
# Launch the Gradio appif __name__ == "__main__": demo.launch()123456789101112131415161718192021222324252627282930"""constants.py"""
import os
APP_TITLE = "π€ Gradio Canvas"
APP_HEADER = """<div style="text-align: center;"> <h1>π€ Gradio Canvas</h1> <p> Powered by <a href="https://fireworks.ai">Fireworks AI</a> π and <a href="https://github.com/instructor-ai/instructor">Instructor</a> π¨βπ«</p></div>"""
FIREWORKS_API_KEY = os.getenv("FIREWORKS_API_KEY")
LLM_MAX_TOKENS = 16_384
LLM_MODEL = "accounts/fireworks/models/llama-v3p1-405b-instruct"
LLM_SYSTEM_PROMPT = """Your goal is to generate Python code based on the user's request.
You will receive a user request and optionally some existing code.
You MUST return your response as a JSON with the following fields: `planning`, `full_python_code`, `commentary`.
The `full_python_code` should be a complete Python script that can be executed - no code blocks or other formatting."""123fireworks-ai==0.15.6gradio==5.1.0instructor==1.6.3README.md+40-1
app.pyAdded+236
config.pyAdded+30
requirements.txtAdded+3