Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from langchain.prompts import ChatPromptTemplate
|
| 3 |
-
from langchain_huggingface import HuggingFaceEndpoint
|
| 4 |
from langchain.schema import HumanMessage, SystemMessage, AIMessage
|
|
|
|
| 5 |
import os
|
| 6 |
import time
|
| 7 |
import logging
|
|
@@ -11,18 +11,15 @@ import re
|
|
| 11 |
logging.basicConfig(level=logging.INFO)
|
| 12 |
logger = logging.getLogger(__name__)
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
if
|
| 17 |
-
logger.warning("HUGGINGFACEHUB_API_TOKEN
|
| 18 |
|
| 19 |
-
# --- LLM
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
top_p=0.9,
|
| 24 |
-
max_new_tokens=1024,
|
| 25 |
-
huggingfacehub_api_token=os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
| 26 |
)
|
| 27 |
math_template = ChatPromptTemplate.from_messages([
|
| 28 |
("system", """{system_message}
|
|
@@ -131,12 +128,20 @@ def respond_with_enhanced_streaming(message, history):
|
|
| 131 |
|
| 132 |
logger.info(f"Processing {mode} query: {message[:50]}...")
|
| 133 |
|
| 134 |
-
# Use
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
"
|
| 138 |
-
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
response = smart_truncate(response, max_length=3000)
|
| 142 |
|
|
@@ -157,7 +162,7 @@ def respond_with_enhanced_streaming(message, history):
|
|
| 157 |
yield final_response
|
| 158 |
|
| 159 |
except Exception as e:
|
| 160 |
-
logger.exception("Error in
|
| 161 |
yield f"Sorry, I encountered an error: {str(e)}"
|
| 162 |
|
| 163 |
# --- Fixed Gradio UI and CSS ---
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from langchain.prompts import ChatPromptTemplate
|
|
|
|
| 3 |
from langchain.schema import HumanMessage, SystemMessage, AIMessage
|
| 4 |
+
from huggingface_hub import InferenceClient
|
| 5 |
import os
|
| 6 |
import time
|
| 7 |
import logging
|
|
|
|
| 11 |
logging.basicConfig(level=logging.INFO)
|
| 12 |
logger = logging.getLogger(__name__)
|
| 13 |
|
| 14 |
+
# Support both token names for flexibility
|
| 15 |
+
hf_token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACEHUB_API_TOKEN")
|
| 16 |
+
if not hf_token:
|
| 17 |
+
logger.warning("Neither HF_TOKEN nor HUGGINGFACEHUB_API_TOKEN is set, the application may not work.")
|
| 18 |
|
| 19 |
+
# --- LLM Configuration ---
|
| 20 |
+
client = InferenceClient(
|
| 21 |
+
provider="together",
|
| 22 |
+
api_key=hf_token,
|
|
|
|
|
|
|
|
|
|
| 23 |
)
|
| 24 |
math_template = ChatPromptTemplate.from_messages([
|
| 25 |
("system", """{system_message}
|
|
|
|
| 128 |
|
| 129 |
logger.info(f"Processing {mode} query: {message[:50]}...")
|
| 130 |
|
| 131 |
+
# Use LangChain template to format the prompt
|
| 132 |
+
formatted_prompt = template.format(
|
| 133 |
+
question=message,
|
| 134 |
+
system_message="You are EduBot, an expert AI learning assistant. Provide comprehensive, educational responses that help students truly understand concepts."
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
# Use the Together provider with text_generation
|
| 138 |
+
response = client.text_generation(
|
| 139 |
+
formatted_prompt,
|
| 140 |
+
model="moonshotai/Kimi-K2-Instruct",
|
| 141 |
+
max_new_tokens=1024,
|
| 142 |
+
temperature=0.7,
|
| 143 |
+
top_p=0.9,
|
| 144 |
+
)
|
| 145 |
|
| 146 |
response = smart_truncate(response, max_length=3000)
|
| 147 |
|
|
|
|
| 162 |
yield final_response
|
| 163 |
|
| 164 |
except Exception as e:
|
| 165 |
+
logger.exception("Error in response generation")
|
| 166 |
yield f"Sorry, I encountered an error: {str(e)}"
|
| 167 |
|
| 168 |
# --- Fixed Gradio UI and CSS ---
|