Sachi Wagaarachchi
commited on
Commit
·
8483978
1
Parent(s):
24e6560
refactor update
Browse files- app.py +0 -64
- pyproject.toml +19 -0
- requirements.txt +0 -1
- run.py +10 -0
- src/__init__.py +0 -0
- src/app.py +72 -0
- src/chat_logic.py +56 -0
- src/gradio_qwen_app.egg-info/PKG-INFO +10 -0
- src/gradio_qwen_app.egg-info/SOURCES.txt +7 -0
- src/gradio_qwen_app.egg-info/dependency_links.txt +1 -0
- src/gradio_qwen_app.egg-info/requires.txt +5 -0
- src/gradio_qwen_app.egg-info/top_level.txt +1 -0
- src/models.py +30 -0
- src/utils.py +13 -0
- src/vector_db.py +9 -0
app.py
DELETED
@@ -1,64 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from huggingface_hub import InferenceClient
|
3 |
-
|
4 |
-
"""
|
5 |
-
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
|
6 |
-
"""
|
7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
-
|
9 |
-
|
10 |
-
def respond(
|
11 |
-
message,
|
12 |
-
history: list[tuple[str, str]],
|
13 |
-
system_message,
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
-
|
20 |
-
for val in history:
|
21 |
-
if val[0]:
|
22 |
-
messages.append({"role": "user", "content": val[0]})
|
23 |
-
if val[1]:
|
24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
25 |
-
|
26 |
-
messages.append({"role": "user", "content": message})
|
27 |
-
|
28 |
-
response = ""
|
29 |
-
|
30 |
-
for message in client.chat_completion(
|
31 |
-
messages,
|
32 |
-
max_tokens=max_tokens,
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
-
|
39 |
-
response += token
|
40 |
-
yield response
|
41 |
-
|
42 |
-
|
43 |
-
"""
|
44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
45 |
-
"""
|
46 |
-
demo = gr.ChatInterface(
|
47 |
-
respond,
|
48 |
-
additional_inputs=[
|
49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
-
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
-
],
|
60 |
-
)
|
61 |
-
|
62 |
-
|
63 |
-
if __name__ == "__main__":
|
64 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pyproject.toml
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
[build-system]
|
2 |
+
requires = ["setuptools", "wheel"]
|
3 |
+
build-backend = "setuptools.build_meta"
|
4 |
+
|
5 |
+
[project]
|
6 |
+
name = "gradio-qwen-app"
|
7 |
+
version = "0.1.0"
|
8 |
+
description = "Gradio app with Qwen models"
|
9 |
+
requires-python = ">=3.12"
|
10 |
+
dependencies = [
|
11 |
+
"gradio>=4.0.0",
|
12 |
+
"transformers>=4.38.0",
|
13 |
+
"torch>=2.0.0",
|
14 |
+
"accelerate>=0.25.0",
|
15 |
+
"huggingface_hub==0.25.2",
|
16 |
+
]
|
17 |
+
|
18 |
+
[tool.setuptools]
|
19 |
+
packages.find.where = ["src"]
|
requirements.txt
DELETED
@@ -1 +0,0 @@
|
|
1 |
-
huggingface_hub==0.25.2
|
|
|
|
run.py
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python3
|
2 |
+
"""
|
3 |
+
Run script for the Gradio Qwen application.
|
4 |
+
This script imports and launches the Gradio app from the src package.
|
5 |
+
"""
|
6 |
+
|
7 |
+
from src.app import demo
|
8 |
+
|
9 |
+
if __name__ == "__main__":
|
10 |
+
demo.launch()
|
src/__init__.py
ADDED
File without changes
|
src/app.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from src.models import ModelManager
|
3 |
+
from src.chat_logic import ChatProcessor
|
4 |
+
from src.vector_db import VectorDBHandler
|
5 |
+
import logging
|
6 |
+
|
7 |
+
# Initialize components
|
8 |
+
model_manager = ModelManager()
|
9 |
+
vector_db = VectorDBHandler()
|
10 |
+
chat_processor = ChatProcessor(model_manager, vector_db)
|
11 |
+
|
12 |
+
# Configure logging
|
13 |
+
logging.basicConfig(level=logging.INFO)
|
14 |
+
logger = logging.getLogger(__name__)
|
15 |
+
|
16 |
+
def respond(
|
17 |
+
message,
|
18 |
+
history: list[tuple[str, str]],
|
19 |
+
model_name: str,
|
20 |
+
system_message: str = "You are a Qwen3 assistant.",
|
21 |
+
max_new_tokens: int = 512,
|
22 |
+
temperature: float = 0.7,
|
23 |
+
top_p: float = 0.9,
|
24 |
+
top_k: int = 50,
|
25 |
+
repetition_penalty: float = 1.2
|
26 |
+
):
|
27 |
+
"""Process chat using the ChatProcessor with streaming support"""
|
28 |
+
try:
|
29 |
+
# Process chat through ChatProcessor
|
30 |
+
response_generator = chat_processor.process_chat(
|
31 |
+
message=message,
|
32 |
+
history=history,
|
33 |
+
model_name=model_name,
|
34 |
+
temperature=temperature,
|
35 |
+
max_new_tokens=max_new_tokens,
|
36 |
+
top_p=top_p,
|
37 |
+
top_k=top_k,
|
38 |
+
repetition_penalty=repetition_penalty
|
39 |
+
)
|
40 |
+
|
41 |
+
# Stream response tokens
|
42 |
+
response = ""
|
43 |
+
for token in response_generator:
|
44 |
+
response += token
|
45 |
+
yield response
|
46 |
+
|
47 |
+
except Exception as e:
|
48 |
+
logger.error(f"Chat response error: {str(e)}")
|
49 |
+
yield f"Error: {str(e)}"
|
50 |
+
|
51 |
+
|
52 |
+
# Create Gradio interface
|
53 |
+
demo = gr.ChatInterface(
|
54 |
+
respond,
|
55 |
+
additional_inputs=[
|
56 |
+
gr.Dropdown(
|
57 |
+
choices=["Qwen3-14B", "Qwen3-7B"],
|
58 |
+
value="Qwen3-7B",
|
59 |
+
label="Model Selection"
|
60 |
+
),
|
61 |
+
gr.Textbox(value="You are a Qwen3 assistant.", label="System message"),
|
62 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
63 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
64 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.9, step=0.05, label="Top-p"),
|
65 |
+
gr.Slider(minimum=1, maximum=100, value=50, step=1, label="Top-k"),
|
66 |
+
gr.Slider(minimum=1.0, maximum=2.0, value=1.2, step=0.1, label="Repetition penalty")
|
67 |
+
],
|
68 |
+
)
|
69 |
+
|
70 |
+
|
71 |
+
if __name__ == "__main__":
|
72 |
+
demo.launch()
|
src/chat_logic.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import TextIteratorStreamer
|
2 |
+
import threading
|
3 |
+
from src.utils import format_prompt
|
4 |
+
import logging
|
5 |
+
|
6 |
+
class ChatProcessor:
|
7 |
+
"""Processes chat interactions using Qwen models"""
|
8 |
+
def __init__(self, model_manager, vector_db):
|
9 |
+
self.model_manager = model_manager
|
10 |
+
self.vector_db = vector_db
|
11 |
+
self.logger = logging.getLogger(__name__)
|
12 |
+
|
13 |
+
def process_chat(self, message, history, model_name, temperature=0.7,
|
14 |
+
max_new_tokens=512, top_p=0.9, top_k=50, repetition_penalty=1.2):
|
15 |
+
"""Process chat input and generate streaming response"""
|
16 |
+
try:
|
17 |
+
# Format prompt with history
|
18 |
+
prompt = format_prompt(message, history)
|
19 |
+
|
20 |
+
# Get model pipeline
|
21 |
+
pipe = self.model_manager.get_pipeline(model_name)
|
22 |
+
|
23 |
+
# Set up streamer
|
24 |
+
streamer = TextIteratorStreamer(
|
25 |
+
pipe.tokenizer,
|
26 |
+
skip_prompt=True,
|
27 |
+
skip_special_tokens=True
|
28 |
+
)
|
29 |
+
|
30 |
+
# Prepare generation kwargs
|
31 |
+
generate_kwargs = {
|
32 |
+
"input_ids": pipe.tokenizer(prompt, return_tensors="pt").input_ids,
|
33 |
+
"max_new_tokens": max_new_tokens,
|
34 |
+
"temperature": temperature,
|
35 |
+
"top_p": top_p,
|
36 |
+
"top_k": top_k,
|
37 |
+
"repetition_penalty": repetition_penalty,
|
38 |
+
"streamer": streamer
|
39 |
+
}
|
40 |
+
|
41 |
+
# Start generation thread
|
42 |
+
thread = threading.Thread(target=pipe.model.generate, kwargs=generate_kwargs)
|
43 |
+
thread.start()
|
44 |
+
|
45 |
+
# Stream response
|
46 |
+
response = ""
|
47 |
+
for token in streamer:
|
48 |
+
response += token
|
49 |
+
yield token
|
50 |
+
|
51 |
+
# Update history (handled by Gradio UI)
|
52 |
+
return response
|
53 |
+
|
54 |
+
except Exception as e:
|
55 |
+
self.logger.error(f"Chat processing error: {str(e)}")
|
56 |
+
yield f"Error: {str(e)}"
|
src/gradio_qwen_app.egg-info/PKG-INFO
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Metadata-Version: 2.4
|
2 |
+
Name: gradio-qwen-app
|
3 |
+
Version: 0.1.0
|
4 |
+
Summary: Gradio app with Qwen models
|
5 |
+
Requires-Python: >=3.12
|
6 |
+
Requires-Dist: gradio>=4.0.0
|
7 |
+
Requires-Dist: transformers>=4.38.0
|
8 |
+
Requires-Dist: torch>=2.0.0
|
9 |
+
Requires-Dist: accelerate>=0.25.0
|
10 |
+
Requires-Dist: huggingface_hub==0.25.2
|
src/gradio_qwen_app.egg-info/SOURCES.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
README.md
|
2 |
+
pyproject.toml
|
3 |
+
src/gradio_qwen_app.egg-info/PKG-INFO
|
4 |
+
src/gradio_qwen_app.egg-info/SOURCES.txt
|
5 |
+
src/gradio_qwen_app.egg-info/dependency_links.txt
|
6 |
+
src/gradio_qwen_app.egg-info/requires.txt
|
7 |
+
src/gradio_qwen_app.egg-info/top_level.txt
|
src/gradio_qwen_app.egg-info/dependency_links.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
|
src/gradio_qwen_app.egg-info/requires.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio>=4.0.0
|
2 |
+
transformers>=4.38.0
|
3 |
+
torch>=2.0.0
|
4 |
+
accelerate>=0.25.0
|
5 |
+
huggingface_hub==0.25.2
|
src/gradio_qwen_app.egg-info/top_level.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
|
src/models.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import pipeline
|
2 |
+
import logging
|
3 |
+
|
4 |
+
class ModelManager:
|
5 |
+
"""Manages loading and caching of Qwen models"""
|
6 |
+
def __init__(self):
|
7 |
+
self.models = {
|
8 |
+
"Qwen3-14B": "Qwen/Qwen3-14B",
|
9 |
+
"Qwen3-7B": "Qwen/Qwen3-7B"
|
10 |
+
}
|
11 |
+
self._pipelines = {}
|
12 |
+
self.logger = logging.getLogger(__name__)
|
13 |
+
|
14 |
+
def get_pipeline(self, model_name):
|
15 |
+
"""Get or create a model pipeline"""
|
16 |
+
if model_name in self._pipelines:
|
17 |
+
return self._pipelines[model_name]
|
18 |
+
|
19 |
+
try:
|
20 |
+
model_id = self.models[model_name]
|
21 |
+
self.logger.info(f"Loading model: {model_id}")
|
22 |
+
pipe = pipeline(
|
23 |
+
"text-generation",
|
24 |
+
model=model_id,
|
25 |
+
device_map="auto"
|
26 |
+
)
|
27 |
+
self._pipelines[model_name] = pipe
|
28 |
+
return pipe
|
29 |
+
except KeyError:
|
30 |
+
raise ValueError(f"Model {model_name} not found in available models")
|
src/utils.py
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
def format_prompt(message, history):
|
2 |
+
"""Format message and history into a prompt for Qwen models"""
|
3 |
+
if not history:
|
4 |
+
return message
|
5 |
+
|
6 |
+
# Convert history to string format
|
7 |
+
prompt = ""
|
8 |
+
for user_msg, assistant_msg in history:
|
9 |
+
prompt += f"<|User|>: {user_msg}\n<|Assistant|>: {assistant_msg}\n"
|
10 |
+
|
11 |
+
# Add current message
|
12 |
+
prompt += f"<|User|>: {message}\n<|Assistant|>:"
|
13 |
+
return prompt
|
src/vector_db.py
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
class VectorDBHandler:
|
2 |
+
"""Placeholder for vector database operations"""
|
3 |
+
def __init__(self):
|
4 |
+
pass
|
5 |
+
|
6 |
+
def retrieve(self, query, k=5):
|
7 |
+
"""Retrieve relevant documents from vector database"""
|
8 |
+
# Placeholder implementation
|
9 |
+
return []
|