OnlyCheeini commited on
Commit
0d78aa2
1 Parent(s): 7737d3e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -54
app.py CHANGED
@@ -1,54 +1,31 @@
1
- import os
2
- from fastapi import FastAPI, HTTPException
3
- from pydantic import BaseModel
4
- from transformers import AutoModelForCausalLM, AutoTokenizer
5
- import torch
6
- # Disable hf_transfer
7
- os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "false"
8
-
9
- app = FastAPI()
10
-
11
- # Load your fine-tuned model and tokenizer
12
- model_name = "OnlyCheeini/greesychat-turbo"
13
- tokenizer = AutoTokenizer.from_pretrained(model_name)
14
-
15
- # Check if a GPU is available, otherwise use CPU
16
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
17
- model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32).to(device)
18
-
19
- class OpenAIRequest(BaseModel):
20
- model: str
21
- prompt: str
22
- max_tokens: int = 64
23
- temperature: float = 0.7
24
- top_p: float = 0.9
25
-
26
- class OpenAIResponse(BaseModel):
27
- choices: list
28
-
29
- @app.post("/v1/completions", response_model=OpenAIResponse)
30
- async def generate_text(request: OpenAIRequest):
31
- if request.model != model_name:
32
- raise HTTPException(status_code=400, detail="Model not found")
33
-
34
- inputs = tokenizer(request.prompt, return_tensors="pt").to(device)
35
- outputs = model.generate(
36
- **inputs,
37
- max_length=inputs['input_ids'].shape[1] + request.max_tokens,
38
- temperature=request.temperature,
39
- top_p=request.top_p,
40
- )
41
-
42
- generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
43
- return OpenAIResponse(choices=[{"text": generated_text}])
44
-
45
- if __name__ == "__main__":
46
- import uvicorn
47
- import gradio as gr
48
-
49
- def greet(name, req: gr.Request):
50
- return f"{req.headers=}"
51
-
52
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
53
- iface.launch()
54
- uvicorn.run(app, host="0.0.0.0", port=8000)
 
1
+ import gradio as gr
2
+ import subprocess
3
+
4
+ def get_ip(space_url):
5
+ try:
6
+ # Use the ping command to get the IP address
7
+ result = subprocess.run(["ping", "-c", "1", space_url], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
8
+ output = result.stdout
9
+
10
+ # Extract the IP address from the output
11
+ start = output.find("(") + 1
12
+ end = output.find(")")
13
+ ip_address = output[start:end]
14
+
15
+ if not ip_address:
16
+ return "Failed to retrieve IP address."
17
+
18
+ return ip_address
19
+ except Exception as e:
20
+ return str(e)
21
+
22
+ # Gradio interface
23
+ iface = gr.Interface(
24
+ fn=get_ip,
25
+ inputs="text",
26
+ outputs="text",
27
+ title="Get Hugging Face Space IP Address",
28
+ description="Enter the URL of your Hugging Face Space (e.g., your-space-url.hf.space) to retrieve its IP address."
29
+ )
30
+
31
+ iface.launch()