Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,69 +1,112 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
|
|
|
|
|
|
|
|
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
history: list[dict[str, str]],
|
| 8 |
-
system_message,
|
| 9 |
-
max_tokens,
|
| 10 |
-
temperature,
|
| 11 |
-
top_p,
|
| 12 |
-
hf_token: gr.OAuthToken,
|
| 13 |
-
):
|
| 14 |
-
"""
|
| 15 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 16 |
-
"""
|
| 17 |
-
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 18 |
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
|
|
|
| 22 |
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
messages,
|
| 29 |
-
max_tokens=max_tokens,
|
| 30 |
-
stream=True,
|
| 31 |
-
temperature=temperature,
|
| 32 |
-
top_p=top_p,
|
| 33 |
-
):
|
| 34 |
-
choices = message.choices
|
| 35 |
-
token = ""
|
| 36 |
-
if len(choices) and choices[0].delta.content:
|
| 37 |
-
token = choices[0].delta.content
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
"""
|
| 46 |
-
chatbot = gr.ChatInterface(
|
| 47 |
respond,
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
-
],
|
| 60 |
)
|
| 61 |
|
| 62 |
-
with gr.Blocks() as demo:
|
| 63 |
-
with gr.Sidebar():
|
| 64 |
-
gr.LoginButton()
|
| 65 |
-
chatbot.render()
|
| 66 |
-
|
| 67 |
-
|
| 68 |
if __name__ == "__main__":
|
| 69 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from openai import OpenAI
|
| 3 |
+
import os
|
| 4 |
+
import glob
|
| 5 |
+
from pypdf import PdfReader
|
| 6 |
|
| 7 |
+
# Initialize client using Hugging Face routing
|
| 8 |
+
client = OpenAI(
|
| 9 |
+
base_url="https://router.huggingface.co/v1",
|
| 10 |
+
api_key=os.environ.get("HF_TOKEN"),
|
| 11 |
+
)
|
| 12 |
|
| 13 |
+
# --- Document Context Logic ---
|
| 14 |
+
DOCS_DIR = "docs" # Folder for your JMeter cheat sheets, project specs, etc.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
def load_pdf_context():
|
| 17 |
+
"""Reads all PDFs in the DOCS_DIR once at startup and extracts text."""
|
| 18 |
+
context = ""
|
| 19 |
+
if not os.path.exists(DOCS_DIR):
|
| 20 |
+
os.makedirs(DOCS_DIR)
|
| 21 |
+
print(f"Created '{DOCS_DIR}' directory. Upload your PDFs here.")
|
| 22 |
+
return context
|
| 23 |
+
|
| 24 |
+
pdf_files = glob.glob(os.path.join(DOCS_DIR, "*.pdf"))
|
| 25 |
+
if not pdf_files:
|
| 26 |
+
print("No PDFs found in the docs folder.")
|
| 27 |
+
return context
|
| 28 |
+
|
| 29 |
+
print(f"Loading {len(pdf_files)} PDF(s) into context...")
|
| 30 |
+
for file_path in pdf_files:
|
| 31 |
+
try:
|
| 32 |
+
reader = PdfReader(file_path)
|
| 33 |
+
context += f"\n--- Start Document: {os.path.basename(file_path)} ---\n"
|
| 34 |
+
for page in reader.pages:
|
| 35 |
+
text = page.extract_text()
|
| 36 |
+
if text:
|
| 37 |
+
context += text + "\n"
|
| 38 |
+
context += f"--- End Document: {os.path.basename(file_path)} ---\n\n"
|
| 39 |
+
except Exception as e:
|
| 40 |
+
print(f"Error loading {file_path}: {e}")
|
| 41 |
+
|
| 42 |
+
return context
|
| 43 |
|
| 44 |
+
DOCUMENT_CONTEXT = load_pdf_context()
|
| 45 |
+
# -----------------------------------
|
| 46 |
|
| 47 |
+
def respond(message, history):
|
| 48 |
+
# System Message tailored for an experienced Dev learning JMeter
|
| 49 |
+
system_content = (
|
| 50 |
+
"You are a highly experienced Performance Engineer and Apache JMeter expert. "
|
| 51 |
+
"The user you are helping is an experienced software developer who is new to JMeter and stress testing. "
|
| 52 |
+
"Skip basic programming concepts and focus strictly on JMeter architecture (Test Plans, Thread Groups, Samplers, Listeners, Timers), "
|
| 53 |
+
"performance testing theory (Load vs. Stress vs. Spike testing), and best practices (e.g., using JSR223 Samplers with Groovy instead of BeanShell, running in CLI/non-GUI mode). "
|
| 54 |
+
"Be concise, technical, and highly practical. Provide code snippets or XML structures where helpful.\n\n"
|
| 55 |
+
"Here is the project-specific context and documentation you should use to answer questions:\n"
|
| 56 |
+
f"{DOCUMENT_CONTEXT}"
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
messages = [{"role": "system", "content": system_content}]
|
| 60 |
+
|
| 61 |
+
# Add conversation history
|
| 62 |
+
for val in history:
|
| 63 |
+
if val['role'] == 'user':
|
| 64 |
+
messages.append({"role": "user", "content": val['content']})
|
| 65 |
+
else:
|
| 66 |
+
messages.append({"role": "assistant", "content": val['content']})
|
| 67 |
|
| 68 |
+
# Process the current message
|
| 69 |
+
user_content = []
|
| 70 |
+
if message["text"]:
|
| 71 |
+
user_content.append({"type": "text", "text": message["text"]})
|
| 72 |
+
|
| 73 |
+
# Image/File handling placeholder
|
| 74 |
+
for file in message["files"]:
|
| 75 |
+
pass
|
| 76 |
|
| 77 |
+
messages.append({"role": "user", "content": user_content})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
+
response = ""
|
| 80 |
+
|
| 81 |
+
try:
|
| 82 |
+
stream = client.chat.completions.create(
|
| 83 |
+
# You can keep this model or switch to another available on HF-Pro
|
| 84 |
+
model="moonshotai/Kimi-K2.5:together",
|
| 85 |
+
messages=messages,
|
| 86 |
+
stream=True
|
| 87 |
+
)
|
| 88 |
|
| 89 |
+
for chunk in stream:
|
| 90 |
+
if hasattr(chunk, 'choices') and len(chunk.choices) > 0:
|
| 91 |
+
token = chunk.choices[0].delta.content
|
| 92 |
+
if token:
|
| 93 |
+
response += token
|
| 94 |
+
yield response
|
| 95 |
+
except Exception as e:
|
| 96 |
+
yield f"Error encountered: {str(e)} - Please verify the model endpoint or your API token."
|
| 97 |
|
| 98 |
+
# Interface Setup
|
| 99 |
+
demo = gr.ChatInterface(
|
|
|
|
|
|
|
| 100 |
respond,
|
| 101 |
+
multimodal=True,
|
| 102 |
+
title="Apache JMeter Performance Expert ⚙️",
|
| 103 |
+
description="Your technical guide for building robust JMeter test plans, analyzing performance metrics, and executing effective stress tests. Backed by your project documentation.",
|
| 104 |
+
examples=[
|
| 105 |
+
{"text": "What is the difference between a Load Test and a Stress Test?"},
|
| 106 |
+
{"text": "How do I extract a dynamic token from a JSON response to use in my next request?"},
|
| 107 |
+
{"text": "Why should I run my tests in non-GUI mode, and what is the command for it?"}
|
| 108 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
)
|
| 110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
if __name__ == "__main__":
|
| 112 |
+
demo.launch()
|