Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from huggingface_hub import InferenceClient
|
3 |
+
|
4 |
+
# Initialize the Hugging Face Inference Client
|
5 |
+
client = InferenceClient()
|
6 |
+
|
7 |
+
# Function to optimize code or simplify mathematical logic
|
8 |
+
def optimize_or_simplify(input_text, task_type):
|
9 |
+
"""
|
10 |
+
Optimizes a given code snippet or simplifies a mathematical expression based on the selected task.
|
11 |
+
|
12 |
+
Args:
|
13 |
+
input_text (str): The user-provided code or math expression.
|
14 |
+
task_type (str): The type of task ('Code Optimization' or 'Math Simplification').
|
15 |
+
|
16 |
+
Returns:
|
17 |
+
str: The optimized code or simplified mathematical result.
|
18 |
+
"""
|
19 |
+
if task_type == "Code Optimization":
|
20 |
+
prompt = (
|
21 |
+
f"Optimize the following code for performance and readability. "
|
22 |
+
f"Provide detailed suggestions and refactor the code:\n\n{input_text}"
|
23 |
+
)
|
24 |
+
else: # Math Simplification
|
25 |
+
prompt = (
|
26 |
+
f"Simplify the following mathematical expression or algorithm, "
|
27 |
+
f"ensuring accuracy and efficiency:\n\n{input_text}"
|
28 |
+
)
|
29 |
+
|
30 |
+
# Call the Hugging Face model
|
31 |
+
response = client.chat.completions.create(
|
32 |
+
model="Qwen/Qwen2.5-Coder-32B-Instruct",
|
33 |
+
messages=[{"role": "user", "content": prompt}],
|
34 |
+
temperature=0.5,
|
35 |
+
max_tokens=512
|
36 |
+
)
|
37 |
+
return response["choices"][0]["message"]["content"]
|
38 |
+
|
39 |
+
# Create Gradio interface
|
40 |
+
with gr.Blocks() as app:
|
41 |
+
gr.Markdown("## Code Optimization and Math Simplification Assistant")
|
42 |
+
gr.Markdown(
|
43 |
+
"Refine your code for better performance or simplify complex mathematical expressions effortlessly. "
|
44 |
+
"Choose a task and input your text to get started!"
|
45 |
+
)
|
46 |
+
|
47 |
+
with gr.Row():
|
48 |
+
# Input section
|
49 |
+
with gr.Column():
|
50 |
+
task_type = gr.Radio(
|
51 |
+
choices=["Code Optimization", "Math Simplification"],
|
52 |
+
label="Select Task",
|
53 |
+
value="Code Optimization"
|
54 |
+
)
|
55 |
+
input_text = gr.Textbox(
|
56 |
+
lines=10,
|
57 |
+
label="Input Text",
|
58 |
+
placeholder="Enter your code or mathematical expression here"
|
59 |
+
)
|
60 |
+
process_button = gr.Button("Process")
|
61 |
+
|
62 |
+
# Output section
|
63 |
+
with gr.Column():
|
64 |
+
gr.Markdown("### Output")
|
65 |
+
output_result = gr.Textbox(lines=15, interactive=False)
|
66 |
+
|
67 |
+
# Link button to function
|
68 |
+
process_button.click(
|
69 |
+
fn=optimize_or_simplify,
|
70 |
+
inputs=[input_text, task_type],
|
71 |
+
outputs=output_result
|
72 |
+
)
|
73 |
+
|
74 |
+
# Launch the app
|
75 |
+
app.launch()
|