Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import requests | |
| import json | |
| def generate_markdown(topic, chat_history): | |
| headers = { | |
| "Authorization": f"Bearer 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", | |
| "Content-Type": "application/json" | |
| } | |
| payload = { | |
| "model": "google/gemini-2.5-pro-preview-03-25", | |
| "messages": [ | |
| { | |
| "role": "user", | |
| "content": f"Generate a comprehensive markdown formatted article about {topic}. Include headings, subheadings, bullet points, and code blocks where appropriate." | |
| } | |
| ] | |
| } | |
| response = requests.post( | |
| "https://openrouter.ai/api/v1/chat/completions", | |
| headers=headers, | |
| data=json.dumps(payload) | |
| if response.status_code == 200: | |
| markdown_content = response.json()['choices'][0]['message']['content'] | |
| chat_history.append(((topic,), (markdown_content,))) | |
| return markdown_content, chat_history | |
| with gr.Blocks(title="Markdown Studio") as demo: | |
| gr.Markdown("# Markdown Studio") | |
| gr.Markdown("Enter a topic to generate a markdown article. You can edit the content and download it.") | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| topic_input = gr.Textbox(label="Enter Topic", placeholder="e.g., Quantum Computing") | |
| generate_btn = gr.Button("Generate Markdown", variant="primary") | |
| chatbot = gr.Chatbot(label="Chat History", height=300) | |
| clear_chat = gr.Button("Clear Chat") | |
| with gr.Column(scale=2): | |
| markdown_editor = gr.Code(label="Markdown Editor", language="markdown", interactive=True) | |
| download_btn = gr.Button("Download as .md") | |
| preview = gr.Markdown(label="Preview") | |
| # Event handlers | |
| generate_btn.click( | |
| fn=generate_markdown, | |
| inputs=[topic_input, chatbot], | |
| outputs=[markdown_editor, chatbot] | |
| ) | |
| markdown_editor.change( | |
| fn=lambda x: x, | |
| inputs=markdown_editor, | |
| outputs=preview | |
| ) | |
| download_btn.click( | |
| fn=lambda x: x, | |
| inputs=markdown_editor, | |
| outputs=gr.File(label="Download Markdown", value=lambda x: ("generated.md", x)) | |
| ) | |
| clear_chat.click( | |
| fn=lambda: [], | |
| inputs=None, | |
| outputs=chatbot, | |
| queue=False | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() |