Spaces:
Sleeping
Sleeping
sinuouspig
commited on
Commit
•
14549cf
1
Parent(s):
28c4926
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
from langchain_groq import ChatGroq
|
4 |
+
from langchain_core.prompts import ChatPromptTemplate
|
5 |
+
from langchain.schema import AIMessage, HumanMessage
|
6 |
+
from langchain_core.output_parsers import StrOutputParser
|
7 |
+
|
8 |
+
MAX_TOKENS = os.getenv("MAX_TOKENS")
|
9 |
+
if MAX_TOKENS is None:
|
10 |
+
MAX_TOKENS = "8192"
|
11 |
+
|
12 |
+
MAX_TOKENS = int(MAX_TOKENS)
|
13 |
+
|
14 |
+
VERSION = "v1.3.1"
|
15 |
+
AI_NAME = "Qbot"
|
16 |
+
|
17 |
+
TEMPLATES = [
|
18 |
+
(
|
19 |
+
"Q&A", """
|
20 |
+
Answer the following questions as best you can.
|
21 |
+
Question: {input}
|
22 |
+
Answer: Respond in the same language as the question.
|
23 |
+
"""
|
24 |
+
),
|
25 |
+
(
|
26 |
+
"Summarization", """
|
27 |
+
Summarize the following text into one sentence.
|
28 |
+
text: {input}
|
29 |
+
"""
|
30 |
+
),
|
31 |
+
]
|
32 |
+
|
33 |
+
GROQ_MODELS = [
|
34 |
+
"llama3-70b-8192",
|
35 |
+
"mixtral-8x7b-32768",
|
36 |
+
"gemma-7b-it",
|
37 |
+
"llama3-8b-8192",
|
38 |
+
]
|
39 |
+
|
40 |
+
|
41 |
+
async def predict(message, history, model_name, template, temperature, max_tokens):
|
42 |
+
llm = ChatGroq(model_name=model_name, temperature=temperature,
|
43 |
+
streaming=True, max_tokens=max_tokens)
|
44 |
+
langchain_history = []
|
45 |
+
for human, ai in history:
|
46 |
+
langchain_history.append(HumanMessage(content=human))
|
47 |
+
langchain_history.append(AIMessage(content=ai))
|
48 |
+
prompt_template = ChatPromptTemplate.from_messages(
|
49 |
+
langchain_history + [("human", template)])
|
50 |
+
|
51 |
+
chain = prompt_template | llm | StrOutputParser()
|
52 |
+
msg = ""
|
53 |
+
async for chunk in chain.astream({"input": message}):
|
54 |
+
msg = msg + chunk
|
55 |
+
yield msg
|
56 |
+
|
57 |
+
|
58 |
+
demo = gr.ChatInterface(
|
59 |
+
fn=predict,
|
60 |
+
description="A Gradio chatbot powered by GroqCloud and Langchain " + VERSION,
|
61 |
+
additional_inputs_accordion=gr.Accordion(
|
62 |
+
label="Parameters", render=False, open=False),
|
63 |
+
additional_inputs=[
|
64 |
+
gr.Dropdown(label="Model", choices=GROQ_MODELS, value=GROQ_MODELS[0]),
|
65 |
+
gr.Dropdown(label="Prompt template",
|
66 |
+
choices=TEMPLATES, value=TEMPLATES[0][1]),
|
67 |
+
gr.Slider(label="Temperature", minimum=0.0, maximum=1.0, step=0.1,
|
68 |
+
value=0.6),
|
69 |
+
gr.Number(label="Max Tokens", value=MAX_TOKENS,
|
70 |
+
step=1024, minimum=1024, maximum=MAX_TOKENS),
|
71 |
+
],
|
72 |
+
examples=[
|
73 |
+
["What is Decision Tree Regression"],
|
74 |
+
["Wite a love story with about 10000 words."],
|
75 |
+
["如何配置 Nginx 多域名和 SSL"],
|
76 |
+
["llm の事前トレーニングと微調整とは何ですか?またその違いは何ですか"],
|
77 |
+
],
|
78 |
+
cache_examples=False,
|
79 |
+
title=AI_NAME
|
80 |
+
)
|
81 |
+
demo.launch()
|