Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,46 +1,42 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
|
| 5 |
# -----------------------------
|
| 6 |
-
#
|
| 7 |
# -----------------------------
|
| 8 |
|
| 9 |
-
|
| 10 |
-
service_name="bedrock-runtime",
|
| 11 |
-
region_name="us-east-1" # change if needed
|
| 12 |
-
)
|
| 13 |
-
|
| 14 |
-
MODEL_ID = "anthropic.claude-3-5-sonnet-20240620-v1:0"
|
| 15 |
|
|
|
|
| 16 |
|
| 17 |
def call_llm(prompt):
|
| 18 |
try:
|
| 19 |
-
|
| 20 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
"messages": [
|
| 22 |
{"role": "user", "content": prompt}
|
| 23 |
],
|
| 24 |
-
"
|
| 25 |
-
"
|
| 26 |
-
}
|
| 27 |
-
|
| 28 |
-
response = bedrock.invoke_model(
|
| 29 |
-
body=body,
|
| 30 |
-
modelId=MODEL_ID,
|
| 31 |
-
contentType="application/json"
|
| 32 |
-
)
|
| 33 |
|
| 34 |
-
|
|
|
|
| 35 |
|
| 36 |
-
return
|
| 37 |
|
| 38 |
except Exception as e:
|
| 39 |
return f"ERROR: {str(e)}"
|
| 40 |
|
| 41 |
|
| 42 |
# -----------------------------
|
| 43 |
-
# BLOCK LOGIC
|
| 44 |
# -----------------------------
|
| 45 |
|
| 46 |
def create_blocks(text):
|
|
@@ -70,6 +66,10 @@ def update_block(blocks, selected, new_text):
|
|
| 70 |
return blocks, gr.update(choices=blocks, value=blocks[idx])
|
| 71 |
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
def ai_rewrite(blocks, selected):
|
| 74 |
if not blocks or not selected:
|
| 75 |
return blocks, gr.update()
|
|
@@ -93,7 +93,7 @@ def ai_summarize(blocks, selected):
|
|
| 93 |
idx = int(selected.split(":")[0])
|
| 94 |
text = blocks[idx].split(": ", 1)[1]
|
| 95 |
|
| 96 |
-
prompt = f"Summarize this in
|
| 97 |
|
| 98 |
return call_llm(prompt)
|
| 99 |
|
|
@@ -102,9 +102,9 @@ def ai_summarize(blocks, selected):
|
|
| 102 |
# UI
|
| 103 |
# -----------------------------
|
| 104 |
|
| 105 |
-
with gr.Blocks(title="Blocksmith +
|
| 106 |
|
| 107 |
-
gr.Markdown("# 🧱 Blocksmith (
|
| 108 |
|
| 109 |
state_blocks = gr.State([])
|
| 110 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
|
| 5 |
# -----------------------------
|
| 6 |
+
# GROK SETUP (xAI)
|
| 7 |
# -----------------------------
|
| 8 |
|
| 9 |
+
API_KEY = os.getenv("XAI_API_KEY")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
API_URL = "https://api.x.ai/v1/chat/completions"
|
| 12 |
|
| 13 |
def call_llm(prompt):
|
| 14 |
try:
|
| 15 |
+
headers = {
|
| 16 |
+
"Authorization": f"Bearer {API_KEY}",
|
| 17 |
+
"Content-Type": "application/json"
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
payload = {
|
| 21 |
+
"model": "grok-beta",
|
| 22 |
"messages": [
|
| 23 |
{"role": "user", "content": prompt}
|
| 24 |
],
|
| 25 |
+
"temperature": 0.7,
|
| 26 |
+
"max_tokens": 500
|
| 27 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 30 |
+
result = response.json()
|
| 31 |
|
| 32 |
+
return result["choices"][0]["message"]["content"]
|
| 33 |
|
| 34 |
except Exception as e:
|
| 35 |
return f"ERROR: {str(e)}"
|
| 36 |
|
| 37 |
|
| 38 |
# -----------------------------
|
| 39 |
+
# BLOCK LOGIC (same as before)
|
| 40 |
# -----------------------------
|
| 41 |
|
| 42 |
def create_blocks(text):
|
|
|
|
| 66 |
return blocks, gr.update(choices=blocks, value=blocks[idx])
|
| 67 |
|
| 68 |
|
| 69 |
+
# -----------------------------
|
| 70 |
+
# AI FUNCTIONS (GROK)
|
| 71 |
+
# -----------------------------
|
| 72 |
+
|
| 73 |
def ai_rewrite(blocks, selected):
|
| 74 |
if not blocks or not selected:
|
| 75 |
return blocks, gr.update()
|
|
|
|
| 93 |
idx = int(selected.split(":")[0])
|
| 94 |
text = blocks[idx].split(": ", 1)[1]
|
| 95 |
|
| 96 |
+
prompt = f"Summarize this in one concise sentence:\n\n{text}"
|
| 97 |
|
| 98 |
return call_llm(prompt)
|
| 99 |
|
|
|
|
| 102 |
# UI
|
| 103 |
# -----------------------------
|
| 104 |
|
| 105 |
+
with gr.Blocks(title="Blocksmith + Grok") as app:
|
| 106 |
|
| 107 |
+
gr.Markdown("# 🧱 Blocksmith (Powered by Grok)")
|
| 108 |
|
| 109 |
state_blocks = gr.State([])
|
| 110 |
|