Spaces:
Sleeping
Sleeping
Ilya
commited on
Commit
β’
298d7d8
1
Parent(s):
c4d5812
Initial
Browse files
app.py
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from huggingface_hub import InferenceClient
|
3 |
+
import sys
|
4 |
+
import io
|
5 |
+
import traceback
|
6 |
+
|
7 |
+
model_name = "Qwen/Qwen2.5-72B-Instruct"
|
8 |
+
client = InferenceClient(model_name)
|
9 |
+
|
10 |
+
def llm_inference(user_sample):
|
11 |
+
eos_token = "<|endoftext|>"
|
12 |
+
output = client.chat.completions.create(
|
13 |
+
messages=[
|
14 |
+
{"role": "system", "content": "You are a Python language guide. Write code on the user topic. Make sure that the code is runnable and doesn't close the shell window, so end with input() if the user request is simple. If the input is code, correct it for mistakes."},
|
15 |
+
{"role": "user", "content": f"Write only python code without any explanation: {user_sample}"},
|
16 |
+
],
|
17 |
+
stream=False,
|
18 |
+
temperature=0.7,
|
19 |
+
top_p=0.1,
|
20 |
+
max_tokens=412,
|
21 |
+
stop=[eos_token]
|
22 |
+
)
|
23 |
+
response = ''
|
24 |
+
for choice in output.choices:
|
25 |
+
response += choice['message']['content']
|
26 |
+
return response
|
27 |
+
|
28 |
+
def chat(user_input, history):
|
29 |
+
response = llm_inference(user_input)
|
30 |
+
history.append((user_input, response))
|
31 |
+
return history, history
|
32 |
+
|
33 |
+
def execute_code(code):
|
34 |
+
old_stdout = sys.stdout
|
35 |
+
redirected_output = sys.stdout = io.StringIO()
|
36 |
+
try:
|
37 |
+
exec(code, {})
|
38 |
+
output = redirected_output.getvalue()
|
39 |
+
except Exception as e:
|
40 |
+
output = f"Error: {e}\n{traceback.format_exc()}"
|
41 |
+
finally:
|
42 |
+
sys.stdout = old_stdout
|
43 |
+
return output
|
44 |
+
|
45 |
+
with gr.Blocks() as demo:
|
46 |
+
gr.Markdown("# π Python Helper Chatbot")
|
47 |
+
with gr.Tab("Chat"):
|
48 |
+
chatbot = gr.Chatbot()
|
49 |
+
msg = gr.Textbox(placeholder="Type your message here...")
|
50 |
+
msg.submit(chat, inputs=[msg, chatbot], outputs=[chatbot, chatbot])
|
51 |
+
with gr.Tab("Interpreter"):
|
52 |
+
gr.Markdown("### π₯οΈ Test Your Code")
|
53 |
+
code_input = gr.Code(language="python")
|
54 |
+
run_button = gr.Button("Run Code")
|
55 |
+
code_output = gr.Textbox(label="Output")
|
56 |
+
run_button.click(execute_code, inputs=code_input, outputs=code_output)
|
57 |
+
with gr.Tab("Logs"):
|
58 |
+
gr.Markdown("### π Logs")
|
59 |
+
log_output = gr.Textbox(label="Logs", lines=10, interactive=False)
|
60 |
+
|
61 |
+
demo.launch()
|