Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,62 +1,35 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
)
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
if val[1]:
|
| 23 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 24 |
-
|
| 25 |
-
messages.append({"role": "user", "content": message})
|
| 26 |
-
|
| 27 |
-
response = ""
|
| 28 |
-
|
| 29 |
-
for message in client.chat_completion(
|
| 30 |
-
messages,
|
| 31 |
-
max_tokens=max_tokens,
|
| 32 |
-
stream=True,
|
| 33 |
-
temperature=temperature,
|
| 34 |
-
top_p=top_p,
|
| 35 |
-
):
|
| 36 |
-
token = message.choices[0].delta.content
|
| 37 |
-
|
| 38 |
-
response += token
|
| 39 |
-
yield response
|
| 40 |
-
|
| 41 |
-
"""
|
| 42 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 43 |
-
"""
|
| 44 |
-
demo = gr.ChatInterface(
|
| 45 |
-
respond,
|
| 46 |
-
additional_inputs=[
|
| 47 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 48 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 49 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 50 |
-
gr.Slider(
|
| 51 |
-
minimum=0.1,
|
| 52 |
-
maximum=1.0,
|
| 53 |
-
value=0.95,
|
| 54 |
-
step=0.05,
|
| 55 |
-
label="Top-p (nucleus sampling)",
|
| 56 |
-
),
|
| 57 |
-
],
|
| 58 |
)
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from transformers.utils import logging
|
| 4 |
+
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
| 5 |
+
import torch
|
| 6 |
+
from llama_index.core import VectorStoreIndex
|
| 7 |
+
from llama_index.core import Document
|
| 8 |
+
from llama_index.core import Settings
|
| 9 |
+
from llama_index.llms.huggingface import (
|
| 10 |
+
HuggingFaceInferenceAPI,
|
| 11 |
+
HuggingFaceLLM,
|
| 12 |
+
)
|
| 13 |
+
Settings.llm = HuggingFaceLLM(model_name="facebook/blenderbot-400M-distill",
|
| 14 |
+
device_map="cpu",
|
| 15 |
+
context_window=128,
|
| 16 |
+
tokenizer_name="facebook/blenderbot-400M-distill"
|
| 17 |
+
)
|
| 18 |
+
Settings.embed_model = HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5")
|
| 19 |
+
documents = [Document(text="Indian parliament elections happened in April-May 2024. BJP Party won.")]
|
| 20 |
+
index = VectorStoreIndex.from_documents(
|
| 21 |
+
documents,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
)
|
| 23 |
|
| 24 |
+
query_engine = index.as_query_engine()
|
| 25 |
+
def rag(input_text, file):
|
| 26 |
+
return query_engine.query(
|
| 27 |
+
input_text
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
iface = gr.Interface(fn=rag, inputs=[gr.Textbox(label="Question", lines=6), gr.File()],
|
| 31 |
+
outputs=[gr.Textbox(label="Result", lines=6)],
|
| 32 |
+
title="Answer my question",
|
| 33 |
+
description= "CoolChatBot"
|
| 34 |
+
)
|
| 35 |
+
iface.launch()
|