S-Dreamer commited on
Commit
78604f3
·
verified ·
1 Parent(s): f40a9d3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -45
app.py CHANGED
@@ -1,50 +1,11 @@
1
  import gradio as gr
2
- from transformers import AutoModelForCausalLM, AutoTokenizer
3
- import torch
4
 
5
- # Load pre-trained Hugging Face model (DialoGPT-medium)
6
- model_name = "microsoft/DialoGPT-medium"
7
- model = AutoModelForCausalLM.from_pretrained(model_name)
8
- tokenizer = AutoTokenizer.from_pretrained(model_name)
9
 
10
- # Chat history to maintain context
11
- chat_history_ids = None
12
 
13
- # Function to generate chatbot response using Hugging Face model
14
- def chatbot_response(user_input, chat_history):
15
- # Tokenize user input and add chat history
16
- new_user_input_ids = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors='pt')
17
 
18
- # Append new input to the chat history
19
- bot_input_ids = torch.cat([chat_history, new_user_input_ids], dim=-1) if chat_history is not None else new_user_input_ids
20
-
21
- # Generate response from the model
22
- chat_history_ids = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id,
23
- temperature=0.7, top_k=50, top_p=0.95, num_return_sequences=1,
24
- no_repeat_ngram_size=3, do_sample=True)
25
-
26
- # Decode the response and return
27
- chat_history_ids = chat_history_ids[:, new_user_input_ids.shape[-1]:] # Keep the new response only
28
- bot_output = tokenizer.decode(chat_history_ids[0], skip_special_tokens=True)
29
-
30
- return bot_output, chat_history_ids
31
-
32
- # Define the Gradio interface
33
- def create_interface():
34
- # Gradio interface setup
35
- with gr.Blocks() as demo:
36
- gr.Markdown("## Chatbot powered by DialoGPT")
37
- with gr.Row():
38
- chat_box = gr.Chatbot()
39
- input_box = gr.Textbox(placeholder="Type a message...")
40
-
41
- # Submit the input and get the response
42
- input_box.submit(chatbot_response, [input_box, chat_box], [chat_box, gr.State()])
43
-
44
- return demo
45
-
46
- # Launch the interface
47
- interface = create_interface()
48
-
49
- # Launch Gradio app on Hugging Face Spaces
50
- interface.launch()
 
1
  import gradio as gr
2
+ from transformers_js_py import pipeline
 
3
 
4
+ pipe = await pipeline('sentiment-analysis')
 
 
 
5
 
6
+ async def process(text):
7
+ return await pipe(text)
8
 
9
+ demo = gr.Interface(fn=process, inputs="text", outputs="json")
 
 
 
10
 
11
+ demo.launch()