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
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
FIREWORKS_API_KEY = os.getenv("FIREWORKS_API_KEY")
|
| 7 |
+
API_URL = "https://api.fireworks.ai/inference/v1/chat/completions"
|
| 8 |
+
MODEL_NAME = "accounts/fireworks/models/glm-4p6"
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
# ---------- Fireworks GLM Request ----------
|
| 12 |
+
def query_fireworks(messages, max_tokens=2048, temperature=0.6):
|
| 13 |
+
headers = {
|
| 14 |
+
"Accept": "application/json",
|
| 15 |
+
"Content-Type": "application/json",
|
| 16 |
+
"Authorization": f"Bearer {FIREWORKS_API_KEY}",
|
| 17 |
+
}
|
| 18 |
+
payload = {
|
| 19 |
+
"model": MODEL_NAME,
|
| 20 |
+
"max_tokens": max_tokens,
|
| 21 |
+
"temperature": temperature,
|
| 22 |
+
"messages": messages,
|
| 23 |
+
}
|
| 24 |
+
response = requests.post(API_URL, headers=headers, data=json.dumps(payload))
|
| 25 |
+
if response.status_code == 200:
|
| 26 |
+
return response.json()["choices"][0]["message"]["content"]
|
| 27 |
+
else:
|
| 28 |
+
return f"Error: {response.text}"
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
# ---------- Documentation Generator ----------
|
| 32 |
+
def generate_docs(code_input_type, code_content, file_obj, github_url, api_key):
|
| 33 |
+
global FIREWORKS_API_KEY
|
| 34 |
+
if api_key:
|
| 35 |
+
FIREWORKS_API_KEY = api_key
|
| 36 |
+
|
| 37 |
+
code = ""
|
| 38 |
+
if code_input_type == "Paste Code":
|
| 39 |
+
code = code_content
|
| 40 |
+
elif code_input_type == "Upload File" and file_obj is not None:
|
| 41 |
+
code = file_obj.read().decode("utf-8")
|
| 42 |
+
elif code_input_type == "GitHub Link" and github_url:
|
| 43 |
+
if "raw.githubusercontent.com" not in github_url:
|
| 44 |
+
github_url = github_url.replace("github.com", "raw.githubusercontent.com").replace("/blob/", "/")
|
| 45 |
+
r = requests.get(github_url)
|
| 46 |
+
if r.status_code == 200:
|
| 47 |
+
code = r.text
|
| 48 |
+
else:
|
| 49 |
+
return f"Unable to fetch file (HTTP {r.status_code})", ""
|
| 50 |
+
|
| 51 |
+
if not code.strip():
|
| 52 |
+
return "β οΈ Please provide valid Python code.", ""
|
| 53 |
+
|
| 54 |
+
messages = [
|
| 55 |
+
{
|
| 56 |
+
"role": "system",
|
| 57 |
+
"content": (
|
| 58 |
+
"You are an AI documentation assistant that analyzes Python code and generates "
|
| 59 |
+
"multi-layer documentation. Include: 1) Overview, 2) Key classes/functions, "
|
| 60 |
+
"3) Usage examples, 4) Dependencies, 5) Suggested improvements."
|
| 61 |
+
),
|
| 62 |
+
},
|
| 63 |
+
{"role": "user", "content": code},
|
| 64 |
+
]
|
| 65 |
+
|
| 66 |
+
output = query_fireworks(messages, max_tokens=4096)
|
| 67 |
+
return output, code
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
# ---------- Code Chat ----------
|
| 71 |
+
def chat_with_code(user_input, chat_history, code_context, api_key):
|
| 72 |
+
global FIREWORKS_API_KEY
|
| 73 |
+
if api_key:
|
| 74 |
+
FIREWORKS_API_KEY = api_key
|
| 75 |
+
|
| 76 |
+
if not code_context.strip():
|
| 77 |
+
return "β οΈ No code context found. Please generate documentation first.", chat_history
|
| 78 |
+
|
| 79 |
+
messages = [
|
| 80 |
+
{
|
| 81 |
+
"role": "system",
|
| 82 |
+
"content": (
|
| 83 |
+
"You are a code analysis assistant. You answer questions about the following Python code. "
|
| 84 |
+
"Explain clearly and refer to specific functions or logic when relevant."
|
| 85 |
+
),
|
| 86 |
+
},
|
| 87 |
+
{"role": "user", "content": f"Code:\n{code_context}\n\nQuestion:\n{user_input}"},
|
| 88 |
+
]
|
| 89 |
+
|
| 90 |
+
reply = query_fireworks(messages)
|
| 91 |
+
chat_history.append(("You", user_input))
|
| 92 |
+
chat_history.append(("GLM-4.6", reply))
|
| 93 |
+
return "", chat_history
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
# ---------- Gradio UI ----------
|
| 97 |
+
|
| 98 |
+
with gr.Blocks(title="Intelligent Documentation Generator Agent") as demo:
|
| 99 |
+
gr.Markdown("# π§ Intelligent Documentation Generator Agent (GLM-4.6 on Fireworks AI)")
|
| 100 |
+
gr.Markdown(
|
| 101 |
+
"Generate intelligent documentation or chat with your Python code. "
|
| 102 |
+
"Provide your Fireworks API key to get started."
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
with gr.Row():
|
| 106 |
+
api_key_box = gr.Textbox(
|
| 107 |
+
label="π Fireworks API Key",
|
| 108 |
+
placeholder="Enter your Fireworks API Key",
|
| 109 |
+
type="password",
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
with gr.Tabs():
|
| 113 |
+
# ---- Documentation Tab ----
|
| 114 |
+
with gr.TabItem("π Generate Documentation"):
|
| 115 |
+
code_input_type = gr.Radio(
|
| 116 |
+
["Paste Code", "Upload File", "GitHub Link"], label="Choose input type", value="Paste Code"
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
code_box = gr.Textbox(label="Paste your Python code", lines=15, visible=True)
|
| 120 |
+
file_upload = gr.File(label="Upload Python file", visible=False)
|
| 121 |
+
github_box = gr.Textbox(label="GitHub raw file link", visible=False)
|
| 122 |
+
|
| 123 |
+
def toggle_inputs(input_type):
|
| 124 |
+
return (
|
| 125 |
+
gr.update(visible=input_type == "Paste Code"),
|
| 126 |
+
gr.update(visible=input_type == "Upload File"),
|
| 127 |
+
gr.update(visible=input_type == "GitHub Link"),
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
code_input_type.change(toggle_inputs, [code_input_type], [code_box, file_upload, github_box])
|
| 131 |
+
|
| 132 |
+
generate_btn = gr.Button("π Generate Documentation")
|
| 133 |
+
|
| 134 |
+
output_box = gr.Markdown(label="π Generated Documentation")
|
| 135 |
+
hidden_code_context = gr.Textbox(visible=False)
|
| 136 |
+
|
| 137 |
+
generate_btn.click(
|
| 138 |
+
generate_docs,
|
| 139 |
+
[code_input_type, code_box, file_upload, github_box, api_key_box],
|
| 140 |
+
[output_box, hidden_code_context],
|
| 141 |
+
)
|
| 142 |
+
|
| 143 |
+
# ---- Chat Tab ----
|
| 144 |
+
with gr.TabItem("π¬ Chat with Code"):
|
| 145 |
+
gr.Markdown("Chat about the Python file you uploaded or analyzed in the previous tab.")
|
| 146 |
+
chatbot = gr.Chatbot(label="GLM-4.6 Code Assistant")
|
| 147 |
+
msg = gr.Textbox(label="Ask a question about the code")
|
| 148 |
+
send_btn = gr.Button("Ask")
|
| 149 |
+
|
| 150 |
+
send_btn.click(
|
| 151 |
+
chat_with_code,
|
| 152 |
+
[msg, chatbot, hidden_code_context, api_key_box],
|
| 153 |
+
[msg, chatbot],
|
| 154 |
+
)
|
| 155 |
+
|
| 156 |
+
demo.launch()
|