Ramzan0553 commited on
Commit
31aa9b2
·
verified ·
1 Parent(s): 3057551

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -0
app.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from huggingface_hub import InferenceClient
4
+
5
+ # Configuration
6
+ HF_TOKEN = os.getenv("HF_TOKEN") # Get Hugging Face token from Hugging Face Secrets
7
+ MODEL_NAME = "microsoft/Phi-3-mini-4k-instruct" # Default model (you can change this if needed)
8
+
9
+ def generate_response(prompt):
10
+ """Generate response using Hugging Face Inference API"""
11
+ try:
12
+ # Initialize InferenceClient with Hugging Face token
13
+ client = InferenceClient(token=HF_TOKEN)
14
+ response = client.text_generation(
15
+ prompt=prompt,
16
+ temperature=0.7,
17
+ top_p=0.9,
18
+ max_new_tokens=800,
19
+ model=MODEL_NAME
20
+ )
21
+ return response
22
+ except Exception as e:
23
+ return f"Error generating response: {str(e)}"
24
+
25
+ def explain_code(code):
26
+ """Generate explanation for provided code"""
27
+ if not code.strip():
28
+ return "Please enter some code to explain."
29
+
30
+ prompt = f"""<|system|>
31
+ You are an AI programming tutor. Explain this code in detail:
32
+ {code}
33
+ Provide a structured explanation covering:
34
+ 1. Overall purpose and functionality
35
+ 2. Key components and their roles
36
+ 3. Flow of execution
37
+ 4. Important patterns or techniques used
38
+ <|end|>
39
+ <|user|>
40
+ Please explain this code<|end|>
41
+ <|assistant|>"""
42
+
43
+ return generate_response(prompt)
44
+
45
+ def create_demo():
46
+ """Create Gradio interface"""
47
+ with gr.Blocks(title="AI Programming Tutor", theme=gr.themes.Soft()) as demo:
48
+ gr.Markdown("""
49
+ # 🤖 AI Programming Tutor
50
+ Get detailed explanations for your code snippets.
51
+ """)
52
+
53
+ with gr.Row():
54
+ with gr.Column():
55
+ code_input = gr.Code(
56
+ label="Your Code",
57
+ value="# Enter your code here...", # Default value instead of placeholder
58
+ language="python",
59
+ lines=10,
60
+ interactive=True
61
+ )
62
+ with gr.Row():
63
+ submit_btn = gr.Button("Explain Code", variant="primary")
64
+ clear_btn = gr.Button("Clear")
65
+
66
+ with gr.Column():
67
+ explanation_output = gr.Textbox(
68
+ label="Code Explanation",
69
+ lines=15,
70
+ interactive=False
71
+ )
72
+
73
+ submit_btn.click(
74
+ fn=explain_code,
75
+ inputs=code_input,
76
+ outputs=explanation_output
77
+ )
78
+
79
+ clear_btn.click(
80
+ fn=lambda: ("# Enter your code here...", ""), # Reset to default value
81
+ inputs=None,
82
+ outputs=[code_input, explanation_output]
83
+ )
84
+
85
+ return demo
86
+
87
+ # Create and launch the interface
88
+ demo = create_demo()
89
+
90
+ if __name__ == "__main__":
91
+ demo.launch()