Spaces:
Running
Running
Upload 3 files
Browse files- app/webui/app.py +4 -4
- app/webui/patch.py +131 -130
- app/webui/process.py +1 -1
app/webui/app.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
import re
|
2 |
import gradio as gr
|
3 |
-
from
|
4 |
from llama_index.core import SimpleDirectoryReader
|
5 |
|
6 |
def huanik(
|
@@ -82,9 +82,9 @@ with gr.Blocks(theme="soft", css=CSS) as demo:
|
|
82 |
endpoint = gr.Dropdown(
|
83 |
label="Endpoint",
|
84 |
choices=["Groq","OpenAI","Cohere","TogetherAI","Ollama","Huggingface"],
|
85 |
-
value="
|
86 |
)
|
87 |
-
model = gr.Textbox(label="Model", value="
|
88 |
api_key = gr.Textbox(label="API_KEY", type="password", )
|
89 |
source_lang = gr.Textbox(
|
90 |
label="Source Lang(Auto-Detect)",
|
@@ -135,7 +135,7 @@ with gr.Blocks(theme="soft", css=CSS) as demo:
|
|
135 |
output_diff = gr.HighlightedText(visible = False)
|
136 |
with gr.Row():
|
137 |
submit = gr.Button(value="Submit")
|
138 |
-
upload = gr.UploadButton("Upload")
|
139 |
clear = gr.ClearButton([source_text, output_init, output_reflect, output_final])
|
140 |
|
141 |
endpoint.change(fn=update_model, inputs=[endpoint], outputs=[model])
|
|
|
1 |
import re
|
2 |
import gradio as gr
|
3 |
+
from .process import model_load, lang_detector, diff_texts, translator
|
4 |
from llama_index.core import SimpleDirectoryReader
|
5 |
|
6 |
def huanik(
|
|
|
82 |
endpoint = gr.Dropdown(
|
83 |
label="Endpoint",
|
84 |
choices=["Groq","OpenAI","Cohere","TogetherAI","Ollama","Huggingface"],
|
85 |
+
value="OpenAI",
|
86 |
)
|
87 |
+
model = gr.Textbox(label="Model", value="gpt-4o", )
|
88 |
api_key = gr.Textbox(label="API_KEY", type="password", )
|
89 |
source_lang = gr.Textbox(
|
90 |
label="Source Lang(Auto-Detect)",
|
|
|
135 |
output_diff = gr.HighlightedText(visible = False)
|
136 |
with gr.Row():
|
137 |
submit = gr.Button(value="Submit")
|
138 |
+
upload = gr.UploadButton("Upload", file_types="text")
|
139 |
clear = gr.ClearButton([source_text, output_init, output_reflect, output_final])
|
140 |
|
141 |
endpoint.change(fn=update_model, inputs=[endpoint], outputs=[model])
|
app/webui/patch.py
CHANGED
@@ -1,131 +1,132 @@
|
|
1 |
-
# a monkey patch to use llama-index completion
|
2 |
-
|
3 |
-
from
|
4 |
-
from
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
from llama_index.llms.
|
9 |
-
from llama_index.llms.
|
10 |
-
from llama_index.llms.
|
11 |
-
from llama_index.llms.
|
12 |
-
from llama_index.llms.
|
13 |
-
|
14 |
-
|
15 |
-
from llama_index.core
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
If json_mode is
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
|
|
131 |
get_completion = completion_wrapper(openai_completion)
|
|
|
1 |
+
# a monkey patch to use llama-index completion
|
2 |
+
import os
|
3 |
+
from typing import Union, Callable
|
4 |
+
from functools import wraps
|
5 |
+
from src.translation_agent.utils import *
|
6 |
+
|
7 |
+
|
8 |
+
from llama_index.llms.groq import Groq
|
9 |
+
from llama_index.llms.cohere import Cohere
|
10 |
+
from llama_index.llms.openai import OpenAI
|
11 |
+
from llama_index.llms.together import TogetherLLM
|
12 |
+
from llama_index.llms.ollama import Ollama
|
13 |
+
from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
|
14 |
+
|
15 |
+
from llama_index.core import Settings
|
16 |
+
from llama_index.core.llms import ChatMessage
|
17 |
+
|
18 |
+
|
19 |
+
# Add your LLMs here
|
20 |
+
|
21 |
+
def model_load(
|
22 |
+
endpoint: str,
|
23 |
+
model: str,
|
24 |
+
api_key: str = None,
|
25 |
+
context_window: int = 4096,
|
26 |
+
num_output: int = 512,
|
27 |
+
):
|
28 |
+
if endpoint == "Groq":
|
29 |
+
llm = Groq(
|
30 |
+
model=model,
|
31 |
+
api_key=api_key,
|
32 |
+
)
|
33 |
+
elif endpoint == "Cohere":
|
34 |
+
llm = Cohere(
|
35 |
+
model=model,
|
36 |
+
api_key=api_key,
|
37 |
+
)
|
38 |
+
elif endpoint == "OpenAI":
|
39 |
+
llm = OpenAI(
|
40 |
+
model=model,
|
41 |
+
api_key=api_key if api_key else os.getenv("OPENAI_API_KEY"),
|
42 |
+
)
|
43 |
+
elif endpoint == "TogetherAI":
|
44 |
+
llm = TogetherLLM(
|
45 |
+
model=model,
|
46 |
+
api_key=api_key,
|
47 |
+
)
|
48 |
+
elif endpoint == "ollama":
|
49 |
+
llm = Ollama(
|
50 |
+
model=model,
|
51 |
+
request_timeout=120.0)
|
52 |
+
elif endpoint == "Huggingface":
|
53 |
+
llm = HuggingFaceInferenceAPI(
|
54 |
+
model_name=model,
|
55 |
+
token=api_key,
|
56 |
+
task="text-generation",
|
57 |
+
)
|
58 |
+
Settings.llm = llm
|
59 |
+
# maximum input size to the LLM
|
60 |
+
Settings.context_window = context_window
|
61 |
+
|
62 |
+
# number of tokens reserved for text generation.
|
63 |
+
Settings.num_output = num_output
|
64 |
+
|
65 |
+
|
66 |
+
|
67 |
+
def completion_wrapper(func: Callable) -> Callable:
|
68 |
+
@wraps(func)
|
69 |
+
def wrapper(
|
70 |
+
prompt: str,
|
71 |
+
system_message: str = "You are a helpful assistant.",
|
72 |
+
temperature: float = 0.3,
|
73 |
+
json_mode: bool = False,
|
74 |
+
) -> Union[str, dict]:
|
75 |
+
"""
|
76 |
+
Generate a completion using the OpenAI API.
|
77 |
+
|
78 |
+
Args:
|
79 |
+
prompt (str): The user's prompt or query.
|
80 |
+
system_message (str, optional): The system message to set the context for the assistant.
|
81 |
+
Defaults to "You are a helpful assistant.".
|
82 |
+
temperature (float, optional): The sampling temperature for controlling the randomness of the generated text.
|
83 |
+
Defaults to 0.3.
|
84 |
+
json_mode (bool, optional): Whether to return the response in JSON format.
|
85 |
+
Defaults to False.
|
86 |
+
|
87 |
+
Returns:
|
88 |
+
Union[str, dict]: The generated completion.
|
89 |
+
If json_mode is True, returns the complete API response as a dictionary.
|
90 |
+
If json_mode is False, returns the generated text as a string.
|
91 |
+
"""
|
92 |
+
llm = Settings.llm
|
93 |
+
if llm.class_name() == "HuggingFaceInferenceAPI":
|
94 |
+
llm.system_prompt = system_message
|
95 |
+
messages = [
|
96 |
+
ChatMessage(
|
97 |
+
role="user", content=prompt),
|
98 |
+
]
|
99 |
+
response = llm.chat(
|
100 |
+
messages=messages,
|
101 |
+
temperature=temperature,
|
102 |
+
top_p=1,
|
103 |
+
)
|
104 |
+
return response.message.content
|
105 |
+
else:
|
106 |
+
messages = [
|
107 |
+
ChatMessage(
|
108 |
+
role="system", content=system_message),
|
109 |
+
ChatMessage(
|
110 |
+
role="user", content=prompt),
|
111 |
+
]
|
112 |
+
|
113 |
+
if json_mode:
|
114 |
+
response = llm.chat(
|
115 |
+
temperature=temperature,
|
116 |
+
top_p=1,
|
117 |
+
response_format={"type": "json_object"},
|
118 |
+
messages=messages,
|
119 |
+
)
|
120 |
+
return response.message.content
|
121 |
+
else:
|
122 |
+
response = llm.chat(
|
123 |
+
temperature=temperature,
|
124 |
+
top_p=1,
|
125 |
+
messages=messages,
|
126 |
+
)
|
127 |
+
return response.message.content
|
128 |
+
|
129 |
+
return wrapper
|
130 |
+
|
131 |
+
openai_completion = get_completion
|
132 |
get_completion = completion_wrapper(openai_completion)
|
app/webui/process.py
CHANGED
@@ -2,7 +2,7 @@ from polyglot.detect import Detector
|
|
2 |
from polyglot.text import Text
|
3 |
from difflib import Differ
|
4 |
from icecream import ic
|
5 |
-
from
|
6 |
from llama_index.core.node_parser import SentenceSplitter
|
7 |
|
8 |
def lang_detector(text):
|
|
|
2 |
from polyglot.text import Text
|
3 |
from difflib import Differ
|
4 |
from icecream import ic
|
5 |
+
from .patch import *
|
6 |
from llama_index.core.node_parser import SentenceSplitter
|
7 |
|
8 |
def lang_detector(text):
|