Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +26 -25
src/streamlit_app.py
CHANGED
|
@@ -84,24 +84,16 @@ def to_text(resp):
|
|
| 84 |
|
| 85 |
|
| 86 |
# --------------------------------------------------------------
|
| 87 |
-
# 6. AUTO-DETECT MODEL
|
| 88 |
# --------------------------------------------------------------
|
| 89 |
-
def
|
| 90 |
-
|
| 91 |
-
r = requests.get(f"{LLM_API}/models", timeout=5)
|
| 92 |
-
if r.status_code == 200:
|
| 93 |
-
models = r.json().get("data", [])
|
| 94 |
-
if models:
|
| 95 |
-
return models[0]["id"]
|
| 96 |
-
except Exception as e:
|
| 97 |
-
st.warning(f"Auto-detect failed: {e}. Using default model.")
|
| 98 |
-
return "Qwen2.5-Coder-7B-Instruct"
|
| 99 |
|
| 100 |
|
| 101 |
# --------------------------------------------------------------
|
| 102 |
-
# 7. CUSTOM LLM
|
| 103 |
# --------------------------------------------------------------
|
| 104 |
-
class
|
| 105 |
model_name: str
|
| 106 |
temperature: float = 0.7
|
| 107 |
context_window: int = 32768
|
|
@@ -110,12 +102,19 @@ class LMStudioLLM(CustomLLM):
|
|
| 110 |
|
| 111 |
def __init__(self, model_name: str, temperature: float = 0.7):
|
| 112 |
super().__init__(model_name=model_name, temperature=temperature)
|
| 113 |
-
self.base_url =
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
@property
|
| 116 |
def metadata(self) -> LLMMetadata:
|
| 117 |
return LLMMetadata(context_window=self.context_window, num_output=self.num_output)
|
| 118 |
|
|
|
|
|
|
|
|
|
|
| 119 |
def complete(self, prompt: str, **kwargs: Any) -> CompletionResponse:
|
| 120 |
payload = {
|
| 121 |
"model": self.model_name,
|
|
@@ -126,14 +125,16 @@ class LMStudioLLM(CustomLLM):
|
|
| 126 |
**kwargs,
|
| 127 |
}
|
| 128 |
try:
|
| 129 |
-
resp = requests.post(
|
| 130 |
resp.raise_for_status()
|
| 131 |
text = resp.json()["choices"][0]["message"]["content"]
|
| 132 |
except Exception as e:
|
| 133 |
-
text = f"[
|
| 134 |
return CompletionResponse(text=text)
|
| 135 |
|
| 136 |
-
#
|
|
|
|
|
|
|
| 137 |
def stream_complete(self, prompt: str, **kwargs: Any) -> Generator[CompletionResponse, None, None]:
|
| 138 |
payload = {
|
| 139 |
"model": self.model_name,
|
|
@@ -143,9 +144,10 @@ class LMStudioLLM(CustomLLM):
|
|
| 143 |
"stream": True,
|
| 144 |
**kwargs,
|
| 145 |
}
|
| 146 |
-
|
| 147 |
try:
|
| 148 |
-
with requests.post(
|
|
|
|
|
|
|
| 149 |
resp.raise_for_status()
|
| 150 |
for line in resp.iter_lines(decode_unicode=True):
|
| 151 |
if not line or not line.startswith("data: "):
|
|
@@ -169,7 +171,6 @@ class LMStudioLLM(CustomLLM):
|
|
| 169 |
async def astream_complete(self, prompt: str, **kwargs: Any) -> AsyncGenerator[CompletionResponse, None]:
|
| 170 |
yield self.complete(prompt, **kwargs)
|
| 171 |
|
| 172 |
-
|
| 173 |
# --------------------------------------------------------------
|
| 174 |
# 8. EXTRACT ZIP (clean old files first)
|
| 175 |
# --------------------------------------------------------------
|
|
@@ -228,12 +229,12 @@ def build_index(_repo_hash: str):
|
|
| 228 |
def main():
|
| 229 |
st.title("π€ AI Codebase β Docs Agent (ModernBERT CPU + LM-Studio Streaming)")
|
| 230 |
|
| 231 |
-
|
| 232 |
-
|
|
|
|
| 233 |
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
llm = LMStudioLLM(model_name=selected_model, temperature=0.7)
|
| 237 |
|
| 238 |
uploaded = st.file_uploader("π¦ Upload GitHub Repo (.zip)", type="zip")
|
| 239 |
if not uploaded:
|
|
|
|
| 84 |
|
| 85 |
|
| 86 |
# --------------------------------------------------------------
|
| 87 |
+
# 6. AUTO-DETECT MODEL (optional β HF Space only has one)
|
| 88 |
# --------------------------------------------------------------
|
| 89 |
+
def get_hf_model():
|
| 90 |
+
return "openai/gpt-4o" # Fixed for your endpoint
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
|
| 93 |
# --------------------------------------------------------------
|
| 94 |
+
# 7. CUSTOM LLM β HUGGING FACE SPACE (streaming + auth)
|
| 95 |
# --------------------------------------------------------------
|
| 96 |
+
class HFChatLLM(CustomLLM):
|
| 97 |
model_name: str
|
| 98 |
temperature: float = 0.7
|
| 99 |
context_window: int = 32768
|
|
|
|
| 102 |
|
| 103 |
def __init__(self, model_name: str, temperature: float = 0.7):
|
| 104 |
super().__init__(model_name=model_name, temperature=temperature)
|
| 105 |
+
self.base_url = "https://siddhjagani-backend.hf.space/v1/chat/completions"
|
| 106 |
+
self.headers = {
|
| 107 |
+
"Authorization": os.getenv("API_KEY"),
|
| 108 |
+
"Content-Type": "application/json",
|
| 109 |
+
}
|
| 110 |
|
| 111 |
@property
|
| 112 |
def metadata(self) -> LLMMetadata:
|
| 113 |
return LLMMetadata(context_window=self.context_window, num_output=self.num_output)
|
| 114 |
|
| 115 |
+
# ------------------------------------------------------------------
|
| 116 |
+
# SYNC COMPLETE
|
| 117 |
+
# ------------------------------------------------------------------
|
| 118 |
def complete(self, prompt: str, **kwargs: Any) -> CompletionResponse:
|
| 119 |
payload = {
|
| 120 |
"model": self.model_name,
|
|
|
|
| 125 |
**kwargs,
|
| 126 |
}
|
| 127 |
try:
|
| 128 |
+
resp = requests.post(self.base_url, headers=self.headers, json=payload, timeout=300)
|
| 129 |
resp.raise_for_status()
|
| 130 |
text = resp.json()["choices"][0]["message"]["content"]
|
| 131 |
except Exception as e:
|
| 132 |
+
text = f"[HF LLM Error]: {e}"
|
| 133 |
return CompletionResponse(text=text)
|
| 134 |
|
| 135 |
+
# ------------------------------------------------------------------
|
| 136 |
+
# STREAMING (token-by-token)
|
| 137 |
+
# ------------------------------------------------------------------
|
| 138 |
def stream_complete(self, prompt: str, **kwargs: Any) -> Generator[CompletionResponse, None, None]:
|
| 139 |
payload = {
|
| 140 |
"model": self.model_name,
|
|
|
|
| 144 |
"stream": True,
|
| 145 |
**kwargs,
|
| 146 |
}
|
|
|
|
| 147 |
try:
|
| 148 |
+
with requests.post(
|
| 149 |
+
self.base_url, headers=self.headers, json=payload, stream=True, timeout=300
|
| 150 |
+
) as resp:
|
| 151 |
resp.raise_for_status()
|
| 152 |
for line in resp.iter_lines(decode_unicode=True):
|
| 153 |
if not line or not line.startswith("data: "):
|
|
|
|
| 171 |
async def astream_complete(self, prompt: str, **kwargs: Any) -> AsyncGenerator[CompletionResponse, None]:
|
| 172 |
yield self.complete(prompt, **kwargs)
|
| 173 |
|
|
|
|
| 174 |
# --------------------------------------------------------------
|
| 175 |
# 8. EXTRACT ZIP (clean old files first)
|
| 176 |
# --------------------------------------------------------------
|
|
|
|
| 229 |
def main():
|
| 230 |
st.title("π€ AI Codebase β Docs Agent (ModernBERT CPU + LM-Studio Streaming)")
|
| 231 |
|
| 232 |
+
# Fixed model
|
| 233 |
+
selected_model = get_hf_model()
|
| 234 |
+
st.info(f"**Using Openai's model:** `{selected_model}`")
|
| 235 |
|
| 236 |
+
# Create LLM
|
| 237 |
+
llm = HFChatLLM(model_name=selected_model, temperature=0.7)
|
|
|
|
| 238 |
|
| 239 |
uploaded = st.file_uploader("π¦ Upload GitHub Repo (.zip)", type="zip")
|
| 240 |
if not uploaded:
|