Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from openai import OpenAI
|
| 3 |
+
|
| 4 |
+
def process_language(api_key, task, user_message):
|
| 5 |
+
if not api_key:
|
| 6 |
+
return "β οΈ Please enter your OpenRouter API Key"
|
| 7 |
+
|
| 8 |
+
client = OpenAI(
|
| 9 |
+
base_url="https://openrouter.ai/api/v1",
|
| 10 |
+
api_key=api_key
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
system_message = ("You are a bilingual language tutor. Respond in the language used in the question. "
|
| 14 |
+
"For conversations, keep exchanges natural. For grammar explanations, provide "
|
| 15 |
+
"detailed rules with English/Chinese examples.")
|
| 16 |
+
|
| 17 |
+
try:
|
| 18 |
+
completion = client.chat.completions.create(
|
| 19 |
+
extra_headers={"HTTP-Referer": "", "X-Title": ""},
|
| 20 |
+
extra_body={},
|
| 21 |
+
model="deepseek/deepseek-v3-base:free",
|
| 22 |
+
messages=[
|
| 23 |
+
{"role": "system", "content": system_message},
|
| 24 |
+
{"role": "user", "content": f"{task}: {user_message}"}
|
| 25 |
+
]
|
| 26 |
+
)
|
| 27 |
+
return completion.choices[0].message.content
|
| 28 |
+
except Exception as e:
|
| 29 |
+
return f"β Error: {str(e)}"
|
| 30 |
+
|
| 31 |
+
def process_pseudocode(api_key, user_idea):
|
| 32 |
+
if not api_key:
|
| 33 |
+
return "β οΈ Please enter your OpenRouter API Key"
|
| 34 |
+
|
| 35 |
+
client = OpenAI(
|
| 36 |
+
base_url="https://openrouter.ai/api/v1",
|
| 37 |
+
api_key=api_key
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
system_message = ("Generate clear, step-by-step pseudocode for the given idea. "
|
| 41 |
+
"Use programming conventions but avoid actual code. Structure your answer with:")
|
| 42 |
+
|
| 43 |
+
try:
|
| 44 |
+
completion = client.chat.completions.create(
|
| 45 |
+
extra_headers={"HTTP-Referer": "", "X-Title": ""},
|
| 46 |
+
extra_body={},
|
| 47 |
+
model="deepseek/deepseek-v3-base:free",
|
| 48 |
+
messages=[
|
| 49 |
+
{"role": "system", "content": system_message},
|
| 50 |
+
{"role": "user", "content": user_idea}
|
| 51 |
+
]
|
| 52 |
+
)
|
| 53 |
+
return completion.choices[0].message.content
|
| 54 |
+
except Exception as e:
|
| 55 |
+
return f"β Error: {str(e)}"
|
| 56 |
+
|
| 57 |
+
def process_algorithm(api_key, challenge):
|
| 58 |
+
if not api_key:
|
| 59 |
+
return "β οΈ Please enter your OpenRouter API Key"
|
| 60 |
+
|
| 61 |
+
client = OpenAI(
|
| 62 |
+
base_url="https://openrouter.ai/api/v1",
|
| 63 |
+
api_key=api_key
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
system_message = ("Suggest multiple approaches to solve the problem. Compare time/space complexity, "
|
| 67 |
+
"trade-offs, and ideal use cases. Structure your answer clearly.")
|
| 68 |
+
|
| 69 |
+
try:
|
| 70 |
+
completion = client.chat.completions.create(
|
| 71 |
+
extra_headers={"HTTP-Referer": "", "X-Title": ""},
|
| 72 |
+
extra_body={},
|
| 73 |
+
model="deepseek/deepseek-v3-base:free",
|
| 74 |
+
messages=[
|
| 75 |
+
{"role": "system", "content": system_message},
|
| 76 |
+
{"role": "user", "content": challenge}
|
| 77 |
+
]
|
| 78 |
+
)
|
| 79 |
+
return completion.choices[0].message.content
|
| 80 |
+
except Exception as e:
|
| 81 |
+
return f"β Error: {str(e)}"
|
| 82 |
+
|
| 83 |
+
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 84 |
+
gr.Markdown("# π Code & Language Learning Assistant")
|
| 85 |
+
api_key = gr.Textbox(label="Enter your OpenRouter API Key", type="password")
|
| 86 |
+
|
| 87 |
+
with gr.Tabs():
|
| 88 |
+
# Language Learning Tab
|
| 89 |
+
with gr.Tab("π¬π§/π¨π³ Language Learning Buddy"):
|
| 90 |
+
with gr.Row():
|
| 91 |
+
with gr.Column():
|
| 92 |
+
task_type = gr.Radio(
|
| 93 |
+
choices=["Simulate bilingual conversation", "Explain grammar rules"],
|
| 94 |
+
label="Select Task Type"
|
| 95 |
+
)
|
| 96 |
+
lang_input = gr.Textbox(label="Your message/question", lines=3)
|
| 97 |
+
lang_btn = gr.Button("Get Assistance")
|
| 98 |
+
lang_output = gr.Textbox(label="Assistant Response", interactive=False, lines=10)
|
| 99 |
+
lang_btn.click(process_language, [api_key, task_type, lang_input], lang_output)
|
| 100 |
+
|
| 101 |
+
# Pseudocode Tab
|
| 102 |
+
with gr.Tab("π Pseudocode Explainer"):
|
| 103 |
+
with gr.Row():
|
| 104 |
+
with gr.Column():
|
| 105 |
+
pseudo_input = gr.Textbox(label="Your idea (e.g., 'How to build a calculator app')", lines=3)
|
| 106 |
+
pseudo_btn = gr.Button("Generate Steps")
|
| 107 |
+
pseudo_output = gr.Textbox(label="Step-by-Step Pseudocode", interactive=False, lines=10)
|
| 108 |
+
pseudo_btn.click(process_pseudocode, [api_key, pseudo_input], pseudo_output)
|
| 109 |
+
|
| 110 |
+
# Algorithm Tab
|
| 111 |
+
with gr.Tab("π§ Algorithm Brainstormer"):
|
| 112 |
+
with gr.Row():
|
| 113 |
+
with gr.Column():
|
| 114 |
+
algo_input = gr.Textbox(label="Challenge description (e.g., 'Sort list without built-in functions')", lines=3)
|
| 115 |
+
algo_btn = gr.Button("Brainstorm Solutions")
|
| 116 |
+
algo_output = gr.Textbox(label="Suggested Approaches", interactive=False, lines=10)
|
| 117 |
+
algo_btn.click(process_algorithm, [api_key, algo_input], algo_output)
|
| 118 |
+
|
| 119 |
+
app.launch()
|