Kailxzz commited on
Commit
0337ce6
1 Parent(s): 24a3c51

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -14
app.py CHANGED
@@ -5,33 +5,82 @@ import os
5
 
6
  # Retrieve the API key from environment variables
7
  API_KEY = os.getenv('API_KEY')
8
- API_URL = "https://api-inference.huggingface.co/models/meta-llama/CodeLlama-7b-hf"
 
 
 
9
  headers = {"Authorization": f"Bearer {API_KEY}"}
10
 
11
- def generate_response(prompt):
12
  data = {
13
- "inputs": prompt
 
 
 
 
14
  }
15
  response = requests.post(API_URL, headers=headers, json=data)
16
  if response.status_code == 200:
17
- return response.json()
 
18
  else:
19
  return f"Error: {response.status_code}\n{response.text}"
20
 
21
- def main(prompt):
22
- response = generate_response(prompt)
23
- if isinstance(response, str):
24
- return response
25
- else:
26
- return json.dumps(response, indent=2)
27
 
 
28
  iface = gr.Interface(
29
  fn=main,
30
- inputs=gr.Textbox(lines=4, placeholder="Enter your prompt here..."),
31
- outputs=gr.Textbox(lines=10),
 
 
 
 
32
  title="Code Assistant",
33
- description="Enter your prompt and get responses from the model."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  )
35
 
 
36
  if __name__ == "__main__":
37
- iface.launch()
 
5
 
6
  # Retrieve the API key from environment variables
7
  API_KEY = os.getenv('API_KEY')
8
+ if not API_KEY:
9
+ raise ValueError("API_KEY environment variable not set")
10
+
11
+ API_URL = "https://api-inference.huggingface.co/models/Salesforce/codegen-350M-mono"
12
  headers = {"Authorization": f"Bearer {API_KEY}"}
13
 
14
+ def generate_response(prompt, max_length=200, temperature=0.5):
15
  data = {
16
+ "inputs": prompt,
17
+ "parameters": {
18
+ "max_length": max_length,
19
+ "temperature": temperature,
20
+ }
21
  }
22
  response = requests.post(API_URL, headers=headers, json=data)
23
  if response.status_code == 200:
24
+ result = response.json()
25
+ return result[0]['generated_text'] if result else "No response received"
26
  else:
27
  return f"Error: {response.status_code}\n{response.text}"
28
 
29
+ def main(prompt, max_length=200, temperature=0.5):
30
+ response = generate_response(prompt, max_length, temperature)
31
+ return response
 
 
 
32
 
33
+ # Enhanced Gradio Interface
34
  iface = gr.Interface(
35
  fn=main,
36
+ inputs=[
37
+ gr.Textbox(label="Enter your prompt", placeholder="Enter your prompt here...", lines=5),
38
+ gr.Slider(label="Max Length", min_value=50, max_value=500, default_value=200, step=10, granularity=1, hide_text=True),
39
+ gr.Slider(label="Temperature", min_value=0.1, max_value=1.0, default_value=0.5, step=0.1, granularity=0.01, hide_text=True)
40
+ ],
41
+ outputs=gr.Textbox(label="Generated Code", placeholder="Generated code will appear here...", lines=15),
42
  title="Code Assistant",
43
+ description="Enter your prompt and get responses from the code generation model. Adjust the max length and temperature to customize the output."
44
+ )
45
+
46
+ # Adding real-time feedback
47
+ def real_time_feedback(prompt):
48
+ # Validate prompt and provide feedback
49
+ if len(prompt) < 10:
50
+ return "Please enter a longer prompt..."
51
+ else:
52
+ return "Ready to generate code!"
53
+
54
+ # Adding rich text formatting
55
+ def format_code(code):
56
+ # Add syntax highlighting to the generated code using HTML tags
57
+ formatted_code = "<pre><code>" + code + "</code></pre>"
58
+ return formatted_code
59
+
60
+ # Adding interactive elements
61
+ def interactive_elements(prompt, max_length=200, temperature=0.5):
62
+ # Provide interactive feedback and formatting
63
+ feedback = real_time_feedback(prompt)
64
+ code = main(prompt, max_length, temperature)
65
+ formatted_code = format_code(code)
66
+ return feedback, formatted_code
67
+
68
+ # Enhanced Gradio Interface with interactive elements
69
+ interactive_iface = gr.Interface(
70
+ fn=interactive_elements,
71
+ inputs=[
72
+ gr.Textbox(label="Enter your prompt", placeholder="Enter your prompt here...", lines=5),
73
+ gr.Slider(label="Max Length", min_value=50, max_value=500, default_value=200, step=10, granularity=1, hide_text=True),
74
+ gr.Slider(label="Temperature", min_value=0.1, max_value=1.0, default_value=0.5, step=0.1, granularity=0.01, hide_text=True)
75
+ ],
76
+ outputs=[
77
+ gr.Textbox(label="Real-Time Feedback", placeholder="Real-time feedback will appear here...", lines=1),
78
+ gr.HTML(label="Generated Code", html=True, placeholder="Generated code will appear here...")
79
+ ],
80
+ title="Enhanced Code Assistant",
81
+ description="Enter your prompt and get responses from the code generation model. Adjust the max length and temperature to customize the output."
82
  )
83
 
84
+ # Launch the Gradio Interface
85
  if __name__ == "__main__":
86
+ interactive_iface.launch()