josephrw commited on
Commit
3c5ff56
·
verified ·
1 Parent(s): 68b18cf

Upload folder using huggingface_hub

Browse files
Files changed (5) hide show
  1. Dockerfile +1 -0
  2. app.py +1 -1
  3. requirements.txt +1 -0
  4. src/builder_llm.py +19 -2
  5. src/observer_llm.py +42 -3
Dockerfile CHANGED
@@ -16,6 +16,7 @@ COPY frontend/ ./frontend/
16
  RUN mkdir -p /app/data/receipts
17
 
18
  ENV PORT=7860
 
19
  EXPOSE 7860
20
 
21
  CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
 
16
  RUN mkdir -p /app/data/receipts
17
 
18
  ENV PORT=7860
19
+ ENV PROVIDER=huggingface
20
  EXPOSE 7860
21
 
22
  CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py CHANGED
@@ -57,7 +57,7 @@ def health():
57
  "ok": True,
58
  "app": "csc-engine",
59
  "version": "1.0.0",
60
- "provider": os.getenv("PROVIDER", "heuristic"),
61
  "sessions_active": len(sessions),
62
  "time": datetime.now(timezone.utc).isoformat(),
63
  }
 
57
  "ok": True,
58
  "app": "csc-engine",
59
  "version": "1.0.0",
60
+ "provider": os.getenv("PROVIDER", "huggingface"),
61
  "sessions_active": len(sessions),
62
  "time": datetime.now(timezone.utc).isoformat(),
63
  }
requirements.txt CHANGED
@@ -5,4 +5,5 @@ pillow>=10.1
5
  numpy>=1.24
6
  requests>=2.31
7
  openai>=1.59
 
8
  python-multipart>=0.0.6
 
5
  numpy>=1.24
6
  requests>=2.31
7
  openai>=1.59
8
+ huggingface_hub>=0.26
9
  python-multipart>=0.0.6
src/builder_llm.py CHANGED
@@ -51,6 +51,21 @@ def call_openai(prompt: str) -> str:
51
  return response.choices[0].message.content
52
 
53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  def build(state: SessionState, observation: dict) -> dict:
55
  """Run builder LLM on observer output.
56
 
@@ -66,13 +81,15 @@ MODE: {state.mode}
66
 
67
  Generate a code patch now."""
68
 
69
- provider = os.getenv("PROVIDER", "ollama").lower().strip()
70
  if provider == "ollama":
71
  output = call_ollama(prompt)
72
  elif provider == "openai":
73
  output = call_openai(prompt)
 
 
74
  else:
75
- raise RuntimeError(f"PROVIDER must be 'ollama' or 'openai', got: {provider!r}. Set PROVIDER env var.")
76
 
77
  # Build patch receipt
78
  patch_hash = sha256_text(output + str(time.time()))
 
51
  return response.choices[0].message.content
52
 
53
 
54
+ def call_hf_inference(prompt: str) -> str:
55
+ """Call HF Inference API for code generation."""
56
+ from huggingface_hub import InferenceClient
57
+
58
+ token = os.getenv("HF_TOKEN") or os.getenv("HUGGING_FACE_HUB_TOKEN")
59
+ client = InferenceClient(token=token)
60
+ model = os.getenv("HF_TEXT_MODEL", "meta-llama/Llama-3.1-8B-Instruct")
61
+ response = client.chat_completion(
62
+ model=model,
63
+ messages=[{"role": "user", "content": prompt}],
64
+ max_tokens=2000,
65
+ )
66
+ return response.choices[0].message.content
67
+
68
+
69
  def build(state: SessionState, observation: dict) -> dict:
70
  """Run builder LLM on observer output.
71
 
 
81
 
82
  Generate a code patch now."""
83
 
84
+ provider = os.getenv("PROVIDER", "huggingface").lower().strip()
85
  if provider == "ollama":
86
  output = call_ollama(prompt)
87
  elif provider == "openai":
88
  output = call_openai(prompt)
89
+ elif provider == "huggingface":
90
+ output = call_hf_inference(prompt)
91
  else:
92
+ raise RuntimeError(f"PROVIDER must be 'ollama', 'openai', or 'huggingface', got: {provider!r}.")
93
 
94
  # Build patch receipt
95
  patch_hash = sha256_text(output + str(time.time()))
src/observer_llm.py CHANGED
@@ -5,6 +5,7 @@ The observer compresses; the builder compiles.
5
  """
6
 
7
  import os
 
8
  import json
9
  import requests
10
  from .stream_state import SessionState
@@ -59,6 +60,42 @@ def call_openai(prompt: str, frame_b64: str | None = None) -> str:
59
  return response.choices[0].message.content
60
 
61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  def observe(state: SessionState, compact_state: dict) -> dict:
63
  """Run observer LLM on compressed state.
64
 
@@ -76,17 +113,19 @@ COMPRESSED SENSORY STATE:
76
 
77
  Produce your observation now."""
78
 
79
- provider = os.getenv("PROVIDER", "ollama").lower().strip()
80
  if provider == "ollama":
81
  output = call_ollama(prompt, frame_b64)
82
  elif provider == "openai":
83
  output = call_openai(prompt, frame_b64)
 
 
84
  else:
85
- raise RuntimeError(f"PROVIDER must be 'ollama' or 'openai', got: {provider!r}. Set PROVIDER env var.")
86
 
87
  return {
88
  "observer_output": output,
89
  "state_hash": compact_state.get("state_hash", ""),
90
  "frame_hash": state.last_frame_hash,
91
- "timestamp": __import__("time").time(),
92
  }
 
5
  """
6
 
7
  import os
8
+ import time
9
  import json
10
  import requests
11
  from .stream_state import SessionState
 
60
  return response.choices[0].message.content
61
 
62
 
63
+ def call_hf_inference(prompt: str, frame_b64: str | None = None) -> str:
64
+ """Call HF Inference API using huggingface_hub InferenceClient.
65
+
66
+ Uses vision-language model when frame is available.
67
+ Falls back to text-only model for state-only observations.
68
+ """
69
+ from huggingface_hub import InferenceClient
70
+
71
+ token = os.getenv("HF_TOKEN") or os.getenv("HUGGING_FACE_HUB_TOKEN")
72
+ client = InferenceClient(token=token)
73
+
74
+ if frame_b64:
75
+ model = os.getenv("HF_VISION_MODEL", "meta-llama/Llama-3.2-11B-Vision-Instruct")
76
+ image_url = f"data:image/jpeg;base64,{frame_b64}"
77
+ response = client.chat_completion(
78
+ model=model,
79
+ messages=[{
80
+ "role": "user",
81
+ "content": [
82
+ {"type": "text", "text": prompt},
83
+ {"type": "image_url", "image_url": {"url": image_url}},
84
+ ],
85
+ }],
86
+ max_tokens=800,
87
+ )
88
+ return response.choices[0].message.content
89
+ else:
90
+ model = os.getenv("HF_TEXT_MODEL", "meta-llama/Llama-3.1-8B-Instruct")
91
+ response = client.chat_completion(
92
+ model=model,
93
+ messages=[{"role": "user", "content": prompt}],
94
+ max_tokens=800,
95
+ )
96
+ return response.choices[0].message.content
97
+
98
+
99
  def observe(state: SessionState, compact_state: dict) -> dict:
100
  """Run observer LLM on compressed state.
101
 
 
113
 
114
  Produce your observation now."""
115
 
116
+ provider = os.getenv("PROVIDER", "huggingface").lower().strip()
117
  if provider == "ollama":
118
  output = call_ollama(prompt, frame_b64)
119
  elif provider == "openai":
120
  output = call_openai(prompt, frame_b64)
121
+ elif provider == "huggingface":
122
+ output = call_hf_inference(prompt, frame_b64)
123
  else:
124
+ raise RuntimeError(f"PROVIDER must be 'ollama', 'openai', or 'huggingface', got: {provider!r}.")
125
 
126
  return {
127
  "observer_output": output,
128
  "state_hash": compact_state.get("state_hash", ""),
129
  "frame_hash": state.last_frame_hash,
130
+ "timestamp": time.time(),
131
  }