ChaoticEconomist commited on
Commit
ee2bd19
·
verified ·
1 Parent(s): 93f8443

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -26
app.py CHANGED
@@ -1,46 +1,42 @@
1
  import gradio as gr
2
- import boto3
3
- import json
4
 
5
  # -----------------------------
6
- # AWS BEDROCK SETUP
7
  # -----------------------------
8
 
9
- bedrock = boto3.client(
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
- body = json.dumps({
20
- "anthropic_version": "bedrock-2023-05-31",
 
 
 
 
 
21
  "messages": [
22
  {"role": "user", "content": prompt}
23
  ],
24
- "max_tokens": 500,
25
- "temperature": 0.7
26
- })
27
-
28
- response = bedrock.invoke_model(
29
- body=body,
30
- modelId=MODEL_ID,
31
- contentType="application/json"
32
- )
33
 
34
- response_body = json.loads(response["body"].read())
 
35
 
36
- return response_body["content"][0]["text"]
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 1 sentence:\n\n{text}"
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 + Bedrock") as app:
106
 
107
- gr.Markdown("# 🧱 Blocksmith (AI Powered via Bedrock)")
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