Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# app.py
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
+
import requests
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
# Load the Hugging Face model and tokenizer
|
7 |
+
model_name = "SakanaAI/Llama-3-8B-Instruct-Coding-Expert"
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
10 |
+
|
11 |
+
# Groq API configuration
|
12 |
+
GROQ_API_KEY = "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"
|
13 |
+
GROQ_API_URL = "https://api.groq.com/v1/completions"
|
14 |
+
|
15 |
+
# Function to query Groq API
|
16 |
+
def query_groq(prompt):
|
17 |
+
headers = {
|
18 |
+
"Authorization": f"Bearer {GROQ_API_KEY}",
|
19 |
+
"Content-Type": "application/json"
|
20 |
+
}
|
21 |
+
data = {
|
22 |
+
"prompt": prompt,
|
23 |
+
"max_tokens": 150
|
24 |
+
}
|
25 |
+
response = requests.post(GROQ_API_URL, headers=headers, json=data)
|
26 |
+
return response.json()["choices"][0]["text"]
|
27 |
+
|
28 |
+
# Function to generate smart contract code
|
29 |
+
def generate_smart_contract(language, requirements):
|
30 |
+
# Create a prompt for the model
|
31 |
+
prompt = f"Generate a {language} smart contract with the following requirements: {requirements}"
|
32 |
+
|
33 |
+
# Use the Hugging Face model to generate code
|
34 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
35 |
+
outputs = model.generate(**inputs, max_length=500)
|
36 |
+
generated_code = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
37 |
+
|
38 |
+
# Enhance the code using Groq API
|
39 |
+
enhanced_code = query_groq(generated_code)
|
40 |
+
|
41 |
+
return enhanced_code
|
42 |
+
|
43 |
+
# Gradio interface for the app
|
44 |
+
def generate_contract(language, requirements):
|
45 |
+
return generate_smart_contract(language, requirements)
|
46 |
+
|
47 |
+
interface = gr.Interface(
|
48 |
+
fn=generate_contract,
|
49 |
+
inputs=["text", "text"],
|
50 |
+
outputs="text",
|
51 |
+
title="Smart Contract Generator",
|
52 |
+
description="Generate smart contracts using AI."
|
53 |
+
)
|
54 |
+
|
55 |
+
# Launch the Gradio app
|
56 |
+
if __name__ == "__main__":
|
57 |
+
interface.launch()
|