ziixh commited on
Commit
0fe9a0c
·
verified ·
1 Parent(s): 67a4038

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -28
app.py CHANGED
@@ -1,45 +1,66 @@
1
  # app.py
2
- from transformers import AutoTokenizer, AutoModelForCausalLM
3
  import requests
4
  import gradio as gr
5
- import torch
6
 
7
- # Load the CodeParrot model and tokenizer (only once)
8
- model_name = "codeparrot/codeparrot-small" # CodeParrot model
9
- tokenizer = AutoTokenizer.from_pretrained(model_name)
10
- model = AutoModelForCausalLM.from_pretrained(model_name)
11
 
12
  # Groq API configuration
13
  GROQ_API_KEY = "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"
14
  GROQ_API_URL = "https://api.groq.com/v1/completions"
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  # Function to query Groq API
17
  def query_groq(prompt):
18
- headers = {
19
- "Authorization": f"Bearer {GROQ_API_KEY}",
20
- "Content-Type": "application/json"
21
- }
22
- data = {
23
- "prompt": prompt,
24
- "max_tokens": 150
25
- }
26
- response = requests.post(GROQ_API_URL, headers=headers, json=data)
27
- return response.json()["choices"][0]["text"]
 
 
 
 
28
 
29
  # Function to generate smart contract code
30
  def generate_smart_contract(language, requirements):
31
- # Create a prompt for the model
32
- prompt = f"Generate a {language} smart contract with the following requirements: {requirements}"
33
-
34
- # Use the CodeParrot model to generate code
35
- inputs = tokenizer(prompt, return_tensors="pt").to("cuda" if torch.cuda.is_available() else "cpu")
36
- outputs = model.generate(**inputs, max_length=300) # Increased max_length for better results
37
- generated_code = tokenizer.decode(outputs[0], skip_special_tokens=True)
38
-
39
- # Enhance the code using Groq API
40
- enhanced_code = query_groq(generated_code)
41
-
42
- return enhanced_code
 
43
 
44
  # Custom CSS for a 3D CGI Figma-like feel
45
  custom_css = """
 
1
  # app.py
 
2
  import requests
3
  import gradio as gr
 
4
 
5
+ # Hugging Face Inference API configuration
6
+ HF_API_KEY = "your_huggingface_api_key" # Replace with your Hugging Face API key
7
+ HF_API_URL = f"https://api-inference.huggingface.co/models/codeparrot/codeparrot-small"
 
8
 
9
  # Groq API configuration
10
  GROQ_API_KEY = "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"
11
  GROQ_API_URL = "https://api.groq.com/v1/completions"
12
 
13
+ # Function to query Hugging Face Inference API
14
+ def query_huggingface(prompt):
15
+ try:
16
+ headers = {
17
+ "Authorization": f"Bearer {HF_API_KEY}",
18
+ "Content-Type": "application/json"
19
+ }
20
+ data = {
21
+ "inputs": prompt,
22
+ "parameters": {
23
+ "max_length": 150 # Limit the length of the generated text
24
+ }
25
+ }
26
+ response = requests.post(HF_API_URL, headers=headers, json=data, timeout=30) # Add timeout
27
+ response.raise_for_status() # Raise an error for bad responses (4xx, 5xx)
28
+ return response.json()[0]["generated_text"]
29
+ except Exception as e:
30
+ return f"Error querying Hugging Face API: {str(e)}"
31
+
32
  # Function to query Groq API
33
  def query_groq(prompt):
34
+ try:
35
+ headers = {
36
+ "Authorization": f"Bearer {GROQ_API_KEY}",
37
+ "Content-Type": "application/json"
38
+ }
39
+ data = {
40
+ "prompt": prompt,
41
+ "max_tokens": 150
42
+ }
43
+ response = requests.post(GROQ_API_URL, headers=headers, json=data, timeout=10) # Add timeout
44
+ response.raise_for_status() # Raise an error for bad responses (4xx, 5xx)
45
+ return response.json()["choices"][0]["text"]
46
+ except Exception as e:
47
+ return f"Error querying Groq API: {str(e)}"
48
 
49
  # Function to generate smart contract code
50
  def generate_smart_contract(language, requirements):
51
+ try:
52
+ # Create a prompt for the model
53
+ prompt = f"Generate a {language} smart contract with the following requirements: {requirements}"
54
+
55
+ # Use Hugging Face Inference API to generate code
56
+ generated_code = query_huggingface(prompt)
57
+
58
+ # Enhance the code using Groq API
59
+ enhanced_code = query_groq(generated_code)
60
+
61
+ return enhanced_code
62
+ except Exception as e:
63
+ return f"Error generating smart contract: {str(e)}"
64
 
65
  # Custom CSS for a 3D CGI Figma-like feel
66
  custom_css = """