Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -68,6 +68,7 @@ if "chunks_store" not in st.session_state:
|
|
| 68 |
index = st.session_state.faiss_index
|
| 69 |
chunks_store = st.session_state.chunks_store
|
| 70 |
|
|
|
|
| 71 |
# ------------------------- Helpers -------------------------
|
| 72 |
def chunk_text(text: str, max_length: int = 500):
|
| 73 |
words, chunks, cur = text.split(), [], []
|
|
@@ -91,24 +92,20 @@ def embed_and_store(chunks):
|
|
| 91 |
|
| 92 |
|
| 93 |
def query_llm(prompt: str) -> str:
|
| 94 |
-
"""
|
| 95 |
try:
|
| 96 |
-
|
| 97 |
model="deepseek-r1-distill-llama-70b",
|
| 98 |
messages=[
|
| 99 |
{"role": "system", "content": SYSTEM_MSG},
|
| 100 |
{"role": "user", "content": prompt},
|
| 101 |
],
|
| 102 |
-
temperature=0.
|
| 103 |
-
max_completion_tokens=
|
| 104 |
top_p=0.9,
|
| 105 |
-
stream=True,
|
| 106 |
-
reasoning_format="hidden", # valid values: 'hidden', 'raw', or 'parsed'
|
| 107 |
)
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
out.append(chunk.choices[0].delta.content or "")
|
| 111 |
-
return "".join(out)
|
| 112 |
except Exception as e:
|
| 113 |
return f"⚠️ **Error:** {str(e)}"
|
| 114 |
|
|
@@ -140,7 +137,7 @@ if uploaded_file:
|
|
| 140 |
context = " ".join(relevant)
|
| 141 |
final_prompt = f"Context:\n{context}\n\nQuestion:\n{user_query}"
|
| 142 |
|
| 143 |
-
with st.spinner("Analyzing…"):
|
| 144 |
answer = query_llm(final_prompt)
|
| 145 |
|
| 146 |
st.markdown("### AI Analysis")
|
|
|
|
| 68 |
index = st.session_state.faiss_index
|
| 69 |
chunks_store = st.session_state.chunks_store
|
| 70 |
|
| 71 |
+
|
| 72 |
# ------------------------- Helpers -------------------------
|
| 73 |
def chunk_text(text: str, max_length: int = 500):
|
| 74 |
words, chunks, cur = text.split(), [], []
|
|
|
|
| 92 |
|
| 93 |
|
| 94 |
def query_llm(prompt: str) -> str:
|
| 95 |
+
"""Non-streaming version for stability on Hugging Face."""
|
| 96 |
try:
|
| 97 |
+
completion = client.chat.completions.create(
|
| 98 |
model="deepseek-r1-distill-llama-70b",
|
| 99 |
messages=[
|
| 100 |
{"role": "system", "content": SYSTEM_MSG},
|
| 101 |
{"role": "user", "content": prompt},
|
| 102 |
],
|
| 103 |
+
temperature=0.5,
|
| 104 |
+
max_completion_tokens=300,
|
| 105 |
top_p=0.9,
|
|
|
|
|
|
|
| 106 |
)
|
| 107 |
+
# Return final text directly
|
| 108 |
+
return completion.choices[0].message.content.strip()
|
|
|
|
|
|
|
| 109 |
except Exception as e:
|
| 110 |
return f"⚠️ **Error:** {str(e)}"
|
| 111 |
|
|
|
|
| 137 |
context = " ".join(relevant)
|
| 138 |
final_prompt = f"Context:\n{context}\n\nQuestion:\n{user_query}"
|
| 139 |
|
| 140 |
+
with st.spinner("Analyzing… please wait."):
|
| 141 |
answer = query_llm(final_prompt)
|
| 142 |
|
| 143 |
st.markdown("### AI Analysis")
|