wmounger commited on
Commit
2e6d447
·
verified ·
1 Parent(s): 8a0cd8f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -19
app.py CHANGED
@@ -1,27 +1,48 @@
1
  import gradio as gr
2
- from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
 
 
3
 
4
- # Load the model
5
- model_name = "microsoft/phi-3-mini-4k-instruct"
6
- tokenizer = AutoTokenizer.from_pretrained(model_name)
7
- model = AutoModelForCausalLM.from_pretrained(model_name)
8
 
9
  def analyze_shampoo(prompt):
10
- # Set up the model pipeline
11
- pipe = pipeline(
12
- "text-generation",
13
- model=model,
14
- tokenizer=tokenizer,
15
- max_new_tokens=2048,
16
- temperature=0.7,
17
- top_p=0.95
18
- )
19
 
20
- # Generate the response
21
- response = pipe(prompt)[0]["generated_text"]
 
 
 
 
 
 
 
22
 
23
- # Return just the generated part (not the input prompt)
24
- return response[len(prompt):]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
  # Create the Gradio interface
27
  interface = gr.Interface(
@@ -29,7 +50,7 @@ interface = gr.Interface(
29
  inputs=gr.Textbox(lines=10, placeholder="Enter your analysis prompt here..."),
30
  outputs=gr.Textbox(lines=20),
31
  title="Phi-3 Shampoo Analyzer",
32
- description="Analyze shampoo ingredients based on user profiles"
33
  )
34
 
35
  interface.launch()
 
1
  import gradio as gr
2
+ import requests
3
+ import json
4
+ import os
5
 
6
+ # Set your API key as an environment variable
7
+ # Important: In the Hugging Face Space, set this as a secret
8
+ HF_API_KEY = os.environ.get("HF_API_KEY", "")
 
9
 
10
  def analyze_shampoo(prompt):
11
+ """
12
+ Send the prompt to the Hugging Face Inference API instead of loading the model locally
13
+ """
14
+ API_URL = "https://api-inference.huggingface.co/models/microsoft/phi-3-mini-4k-instruct"
15
+ headers = {
16
+ "Authorization": f"Bearer {HF_API_KEY}",
17
+ "Content-Type": "application/json"
18
+ }
 
19
 
20
+ payload = {
21
+ "inputs": prompt,
22
+ "parameters": {
23
+ "max_new_tokens": 2048,
24
+ "temperature": 0.7,
25
+ "top_p": 0.95,
26
+ "return_full_text": False
27
+ }
28
+ }
29
 
30
+ try:
31
+ if not HF_API_KEY:
32
+ return "Error: No API key provided. Please add the HF_API_KEY secret to your Space."
33
+
34
+ response = requests.post(API_URL, headers=headers, json=payload)
35
+ response.raise_for_status() # Raise an exception for HTTP errors
36
+
37
+ result = response.json()
38
+ if isinstance(result, list) and len(result) > 0:
39
+ # Extract the generated text
40
+ return result[0].get('generated_text', 'No response generated')
41
+ else:
42
+ return f"Unexpected response format: {result}"
43
+
44
+ except Exception as e:
45
+ return f"Error: {str(e)}"
46
 
47
  # Create the Gradio interface
48
  interface = gr.Interface(
 
50
  inputs=gr.Textbox(lines=10, placeholder="Enter your analysis prompt here..."),
51
  outputs=gr.Textbox(lines=20),
52
  title="Phi-3 Shampoo Analyzer",
53
+ description="Analyze shampoo ingredients based on user profiles using the Hugging Face Inference API"
54
  )
55
 
56
  interface.launch()