Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,42 +1,47 @@
|
|
| 1 |
-
# CodeGuru GenAI App
|
| 2 |
-
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
-
import
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# CodeGuru GenAI App
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import requests
|
| 6 |
+
import json
|
| 7 |
+
|
| 8 |
+
# Load token from environment variable
|
| 9 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 10 |
+
|
| 11 |
+
# Hugging Face Inference API URL for CodeLlama
|
| 12 |
+
HF_API_URL = "https://api-inference.huggingface.co/models/codellama/CodeLlama-7b-Instruct-hf"
|
| 13 |
+
|
| 14 |
+
headers = {
|
| 15 |
+
"Authorization": f"Bearer {HF_TOKEN}",
|
| 16 |
+
"Content-Type": "application/json"
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
history = []
|
| 20 |
+
|
| 21 |
+
def generate_response(prompt):
|
| 22 |
+
history.append(prompt)
|
| 23 |
+
final_prompt = "\n".join(history)
|
| 24 |
+
|
| 25 |
+
payload = {
|
| 26 |
+
"inputs": final_prompt,
|
| 27 |
+
"parameters": {
|
| 28 |
+
"temperature": 1
|
| 29 |
+
}
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
response = requests.post(HF_API_URL, headers=headers, data=json.dumps(payload))
|
| 33 |
+
|
| 34 |
+
if response.status_code == 200:
|
| 35 |
+
result = response.json()
|
| 36 |
+
return result[0]["generated_text"] if isinstance(result, list) else result.get("generated_text", "No response.")
|
| 37 |
+
else:
|
| 38 |
+
return f"Error: {response.status_code} - {response.text}"
|
| 39 |
+
|
| 40 |
+
interface = gr.Interface(
|
| 41 |
+
fn=generate_response,
|
| 42 |
+
inputs=gr.Textbox(lines=4, placeholder="Ask CodeGuru your coding question..."),
|
| 43 |
+
outputs="text",
|
| 44 |
+
title="CodeGuru - Your Code Assistant"
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
interface.launch()
|